GWT's compiler and J2S' compiler are different, even though both are extended from JDT compiler.
GWT's compiler is similar to C++ compiler, compiling *.java to script, calculating the calling map, stripping out all unused or unreferenced methods, linking and obfuscating all scripts into some big files.
Java2Script compiler is similar to Java compiler, compiling each *.java to a *.js file, and compilation is done. All class information is left in *.js without any stripping. To avoid too much *.js HTTP requests, some *.js are packed into a bigger *.z.js file.
In brief, GWT and Java2Script is similar but under different design rules.
For more information about GWT and Java2Script compiler discussion, please read some early posts:
http://groups.google.com/group/Google-Web-Toolkit/browse_thread/thread/9b9cc255ab159586
http://inside.java2script.com/2007/05/14/java-to-_javascript_-compiler-discussion-1.htmlhttp://inside.java2script.com/2007/05/14/java-to-_javascript_-compiler-discussion-2.html
One advantage of Java2Script over GWT would be the feature of dynamic class loading and Java reflection. You can load a class manually by:
AClass.load("org.java2script.demo.ButtonTab", new ARunnable() {
public void run() {
try {
Constructor constructor = getClazz().getConstructor(new Class[] {ControlExample.class});
Object inst = constructor.newInstance(new Object[] {ControlExample.this});
Tab tab = (Tab) inst;
// ... do the following layout job
} catch (Throwable e) {
e.printStackTrace();
throw (Error) e;
}
}
});
The loading procedure is asynchronous. As lazy loading is enabled, it is OK to load complex applications.
Java2Script also supports native _javascript_ inside Java source. It is similar to GWT's JSNI. For examples:
/**
* For java native methods, if Java2Script native codes are given, the
* native method will be generated when Java2Script compiler is enabled
*
* @j2sNative
* var styleCSS = "position:absolute;left:150px;top:5px;width:100px;height:40px;"
* + "text-align:center;font-weight:bold;color:yellow;background-color:blue;"
* + "border:1px solid red;";
* var hiEl = document.createElement("<div style=\"" + styleCSS + "\"></div>");
* document.body.appendChild(hiEl);
* hiEl.appendChild (document.createTextNode ("Hi!"));
*/
native static void sayHi();
You can use @j2s* Javadoc or @J2S* annotation to control which method is visible to _javascript_. For more information, you can visit
http://j2s.sourceforge.net/articles/tutorial-advanced-programming-on-j2s.html
For server communication, Java2Script has Simple RPC, similar to GWT RPC:
SimpleRPCRequest.request(new SendMessage() {
@Override
public void ajaxIn() {
setTo(chatTo.getId());
setMessage(msg);
setThread((String) getData("threadID"));
}
@Override
public void ajaxOut() {
setData("threadID", getThread());
}
@Override
public void ajaxFail() {
System.out.println("Failed to send \"" + sendingMsg + "\"!");
}
});
SendMessage object is an instance of SimpleRPCRunnable. SimpleRPCRunnable extends SimpleSerializable. SimpleSerializable instances will serialize its public members and transport through the Internet. And SimpleRPCRunnable has an ajaxRun() method, which contains native Java codes that are not compiled to _javascript_. #ajaxRun method only visible in desktop mode or in server mode.
There are desktop mode (native Java desktop application) and AJAX browser mode (web RIA application) in Java2Script. In desktop mode, Thread are used to emulate the RPC. In AJAX mode, XMLHttpRequest, <SCRIPT> or <IFrame> is used for the RPC.
For Java language, both of GWT and Java2Script support Java 1.5. And both support JUnit and others.
For development and deployment, both GWT and Java2Script support Eclipse project and both support exporting application as normal Servlet WAR. In Eclipse, you can create Java2Script projects by wizard or make existed Java projects into Java2Script by property page. The Java2Script project will compile both *.class and *.js at the same time. You can run or debug your application as Java application. And you can also run your application as Java2Script application. Java2Script plugin will generate an *.html file to load the specific *.js and run it. After testing both in Java and _javascript_ mode, you can use the build.xml, which is provided by Java2Script, to pack *.class or *.js into *.war. You can deploy the *.war to any given Servlet containers.
Currently, Java2Script has no standalone compiler yet. So you can not build your Java2Script applications outside Eclipse.
From performance aspect, Java2Script applications is not running very fast. It needs to be optimized by applying lazy loading pattern.
For overview of Java2Script architecture, you can visit
http://j2s.sourceforge.net/images/arch-of-j2s.png