Java2Script Getting Started
Java2Script Getting Started
- Aug 16, 2006
This getting started article shows how to run normal Java applications as Java2Script applications.
In this article, we assume you have already successfully installed Java2Script 1.0.0 M3+ following the instructions at, or following the instructions of setting up Java2Script environment at
We assume you already know Java and know how to write and debug normal Java programs inside Eclipse. And we aslo assume you know how to run SWT applications and JUnit tests inside Eclipse.
We do not require you to know anthing about JavaScript and HTML/CSS in this getting started tutorial. But knowing JavaScript and HTML/CSS will be helpful for you to understand the essential parts of Java2Script.
3 Steps for Java2Script's "Hello World"
For your first Java2Script application, It's about 3 steps for you to follow.
Step 1: Create your "Hello World" Java application. You should test your "Hello World" Java application does print "Hello World" before you continue.
Step 2: Enable Java2Script compiler. There is "Java2Script Builder" tab page in Java project's properties page.
Step 3: Select your "Hello World" *.java and run it as "Java2Script Application":
If everything is OK, a "J2S Console" view will be created, and the same "Hello World!" will be printed:
If you would like to test the Java2Script in a standalone browser, try to click "Run in External Browser" button in the "J2S Console" view to launch the Java2Script application in external browser. The external browser may be Firefox, or Internet Explorer, or Opera. Maybe in the standalone browser mode, you can do your debuggings on level of native JavaScript.
If you would like to study how Java2Script works, you can right click inside the J2S console for contextmenu "View Source" item for generated HTML sources. For more technical details, please check out and read the lastest sources from J2S SVN repository or ask questions on j2s-development mail-list.
Running Java2Script SWT Applications
For running SWT applications as "Java2Script Application", you need to add "swt" library to the Java project. To do so, open "Java2Script Builder" page, and select "Resources" in the tree, then click the enabled "Add Libraries" button to open up "Libraries Selection" dialog, select "swt".
Run your SWT application as "Java2Script Application". For example, some windows with different style will be open inside "J2S Console". For example:
Running Java2Script Unit Tests
You can run JUnit tests in Java2Script mode. To do so, you must add the Java2Script "junit" libraries in the "Java2Script Builder" page, and then run JUnit tests as "Java2Script Unit Test". Unlike Eclipse JUnit tests, which are run in JFace/SWT graphical mode, Java2Script unit tests run in text console mode currently.
Java2Script Applications with Referenced Projects
If you split your application into different projects and add required projects on the build path in "Java Build Path" tab page of Java project properties pages, you should also need to "Add Projects" in the "Java2Script Builder" tab page before you run Java2Script application to make everything work correctly.
What Role is Java2Script Playing
Java2Script is aiming at providing a way to convert existed RCP into RIA without any or with small modifications of the original sources. Java2Script is trying not to interrupt the normal desktop application development.
What you have to do with Java2Script is to develop your SWT based applications without thinking about existence of Java2Script. After code debugging and integrated tests, then, you can export your applications into RIA using Java2Script. First, enable Java2Script compiler, second test locally if possible, third deploy them to server and test again. If not everything works correctly, you may need add some native JavaScript modifications of the Java sources to make application as expected in both desktop mode and browser mode. For such native JavaScript modifications, please read Advanced Programming on J2S -- @j2s* and others, or stay tuned with more coming articles.