Chapter 3. Getting Started

 

"Anticipate the difficult by managing the easy"

 Lao Tzu
Table of Contents
A simple Java2Script application
Configuring your Java2Script application launcher.

In this chapter we will build our firsts web aplications using java2script .... TODO

A simple Java2Script application

As with any Java application, the first thing is to create an Eclipse project. But instead of creating a common Java application project, we will create a Java2script application project. To do so, go to menu File -> New... -> Other... and in the dialog select Java2Script Project:

Figure 3-1. Creating a new Java2Script application project

The following steps for finishing creating the Java2Script project are equal to Java project's. Just enter a valid project name and click next/finnish. Congratulations you have created your first Java2Script project!

Note: Java2Script projects are just like common Java projects, but with the Java to JavaScript compiler enabled. This means that your .java files will also be compiled to JavaScript. So the application can be executed inside an HTML document as we will see below. By the way, the generated JavaScript codes will be at the same location as generated .class files, by default at YOU_PROJECT/bin folder.

Now, we will create a simple Java hello world program and see how easily can be executed in an HTML document. First create a Java package like my.first.project and add a Java class named HelloWorld inside it with the following code:


package my.first.project;

public class HelloWorld {

	public static void main(String[] args) {
		System.out.println("hello world");
	}

}
    

Save the file with CTRL + S. Now we will execute the HelloWorld class as a Java2Script application. An HTML document named my.first.project.HelloWorld.html will be created that launch our little Java application. You can launch your Java2Script application in the same way you launch a normal Java application: right click you Java code -> Run As -> Java2Script Application:

Figure 3-2. Launch a Java2Script application

When you run a Java class as a Java2Script Application, the J2S console Eclipse view will be opened and it will show an HTML document which will execute your Java application. This view is a simple browser and in fact you can also open the generated HTML document with your favourite browser:

Figure 3-3. Launch a Java2Script application - the J2S Console view

Congratulations, you have just executed your first Java2Script application! As you can see, java2script simulate the System.out.println method call pending a paragraph with text to the html document. Another thing to notice, is that if you refresh your project, you will see a new html file at the root

Tip: Remember, if you just make a change in a .java file and save the file, the J2S compiler will regenerate the javascript code and you only need to refresh the HTML document in the browser. If you only change a .java file, you don't need to run as.. the application again, only refresh the document in the browser. Try it, copy the url of the html document from the J2S console, and paste it in your stem browser. Then change the System.out string in HelloWorld.java file and save it. Now refresh the browser and you should see the change reflected.