Monday, January 20, 2014

Compiling and creating jar of JSON Java

This is mostly going to be a "note to self" since I'm going to cover some very basic stuff such as:

  1. get code from github
  2. compile Java files into .class files
  3. create jar file
  4. import into Eclipse
I happened to need a JSON parser in a  Java project and chose the reference implementation from site.

$ # 1. clone Git repository (it's like svn checkout)
$ git clone
$ # 2. change directory to the downloaded JSON-java directory
$ cd JSON-java/
$ # 3. add all the .java files in the current and subdirectories into a file called sources.txt
$ find . -type f -name "*.java" > sources.txt
$ # 4. create a directory to place the compiled .class files called build
$ mkdir build
$ # 5. tell Java compiler to read the files from sources.txt and place the compiled files in the build directory
$ javac @sources.txt -d build
$ # 6. change directory to the build directory
$ cd build/
$ # 7. create jar file named JSON-java.jar including all (*) files in the current and child directories
$ jar -cf JSON-java.jar *
$ # 8. copy jar file to Eclipse's WEB-INF/lib/ directory
$ cp JSON-java.jar /path/to/eclipse/workspace/project_name/war/WEB-INF/lib/

After the jar file was copied to /war/WEB-INF/lib/ directory of the Eclipse project, open Eclipse and "include" it:
  1. from Eclipse's menu, open Project > Properties
  2. on the left pane, choose Java Build Path
  3. select Libraries tab
  4. click on Add External JARs button
  5. browse to the /war/WEB-INF/lib/ directory of your project and select JSON-java.jar
  6. click OK to close dialog
  7. in any .java file, in the import statements, try to add the following import:
import org.json.*

If it doesn't become red it means everything is fine. If there's anything wrong, check each step, especially the generation of .class files.


Tuesday, January 14, 2014

Workaround for timeout issue in Titanium's WebView.evalJS method

First of all, yes, I'm using Titanium because I want to save time writing code for each platform and I like JavaScript.

I'm writing an app that uses WebView and came across this bug. At first I thought I would have to either wait or write my own Android module, but this hack gave me the hint I needed: bookmarklets! If you don't remember, a bookmarklet is just a JavaScript code that runs when your browser tries to open a URL that starts with javascript: instead of http://.

The only thing that was needed was to prepend any JavaScript code with javascript: and remove any "junk" as suggested here:

  1. comment lines
  2. convert tabs into spaces
  3. convert multiple spaces in a single space
  4. remove leading and trailing spaces
  5. remove line breaks
After the "cleanup" above, I also thought that it would be necessary to URL-encode the result, but it's NOT necessary to do so with Titanium WebView because somebody else (other then myself) is URL-encoding it under the hood.

So, putting everything together:

var jsCode = "" +

// this line starts with TAB and ends with line break
"\t\t// this is a sample comment line that should be deleted\n" + 

// this line has multiple spaces between "body" and "="
"\t\tvar body                = document.body;\n" +

// this line has leading spaces
"        var contentLength = body.innerHTML.length;\n" +

// this line has trailing spaces
"\t\talert( 'Your BODY tag has ' + contentLength + ' characters!' );    \n";

// remove comment lines
jsCode = jsCode.replace(new RegExp("^[\s\t]*//[^\n]+[\n]","gm"),"");

// convert tabs into spaces
jsCode = jsCode.replace(new RegExp("[\t]","g")," ");

// multiple spaces as single space
jsCode = jsCode.replace(new RegExp("[ ]{2,}","g")," ");

// remove leading and trailing spaces
jsCode = jsCode.replace(new RegExp("^[ ]*|[ ]*$","gm"),"");

// remove line breaks
jsCode = jsCode.replace(new RegExp("[\n]","gm"),"");

var prefix = "javascript:(function(){";
var suffix = "})();";
var finalCode = prefix + jsCode + suffix;


var myWebView = Titanium.UI.createWebView({url:''});
myWebView.addEventListener('load', function(){

var myWindow = Titanium.UI.createWindow();


Thursday, January 9, 2014