Tutorial of J2S in Eclipse (2): Hello Simple User -- More about Hello World

Zhou Renjian
Jan 14, 2006

This article makes further explainations of Java2Script application and its configuration, besides the previous classical "Hello World" example.

Prerequisite

Please read previous tutorial Tutorial of J2S in Eclipse (1): Hello J2S World.

Writing Java Code in Eclipse

In the previous tutorial, "Hello J2S World" is printed in the console. It's useless. How about useful classes?

Coming is class SimpleUser:

private String name;
private String password;
private String email;
private int age;
private int sex;

By using Eclipse JDT, you can easily generate the Setter/Getter methods for the class, and then add the constructor:

public SimpleUser();
public SimpleUser(String name, String password);
public SimpleUser(String name, String password, String email, int age, int sex);
by using the menu "Source":
source contextmenu

You should do all things in less than 1 minute if you are familiar with Eclipse.

Then add two static members:

public static int FEMALE = 2;
public static int MALE = 1;
And then you try to override the existed method "toString()" by typing in "to" and then pressing <Alt> + /, then selecting the "Override toString in Object" item. then write some codes like these:
	public String toString() {
		String str = "This is user " + name + ", ";
		if (age > 0) {
			if (sex == FEMALE) {
				str += "she";
			} else {
				str += "he";
			}
			str += " is " + age + " years old. ";
		} else {
			if (sex == FEMALE) {
				str += "her";
			} else {
				str += "his";
			}
			str += " age is unkown. ";
		}
		if (sex == FEMALE) {
			str += "She";
		} else {
			str += "He";
		}
		if (email != null && email.length() != 0) {
			str += " has an email " + email + ".";
		} else {
			str += " has no email at this time.";
		}
		return str;
	}
Now the class User will describe itself. Here is source of SimpleUser.java

Make a class HelloSimpleUser with the following "main" method:

	public static void main(String[] args) {
		SimpleUser josson = new SimpleUser("josson", "hello", null, 24, SimpleUser.MALE);
		josson.setEmail("josson@smith");
		System.out.println(josson);
		
		SimpleUser kelly = new SimpleUser("kelly", "");
		kelly.setSex(SimpleUser.FEMALE);
		System.out.println(kelly);
	}
Here is source of HelloSimpleUser.java

Run J2S with multiple Java Classes

Now run the HelloSimpleUser both as "Java Application" and "Java2Script Application", you will get result:

This is user josson, he is 24 years old. He has an email josson@smith.
This is user kelly, her age is unkown. She has no email at this time.
in both consoles. You can also visit the online J2S demo of HelloSimpleUser.

Now let's come to a close look at the configuration of running HelloSimpleUser as "Java2Script Application". As the HelloSimpleUser' static main method will create an instance of class SimpleUser, which is represented in file "SimpleUser.js", a classpath should be told to the J2S launcher. What happens?

In the HTML source of net.sf.j2s.hello.HelloSimpleUser.html, there are lines:

<link rel="stylesheet" href="j2slib/console.css"/>
<script type="text/javascript" src="j2slib/j2s-core-common.jz"></script>
<script type="text/javascript" src="bin/net/sf/j2s/hello/HelloJ2SWorld.js"></script>
<script type="text/javascript" src="bin/net/sf/j2s/hello/SimpleUser.js"></script>
<script type="text/javascript" src="bin/net/sf/j2s/hello/HelloSimpleUser.js"></script>
So the SimpleUser.js is loaded before HelloSimpleUser.js. What is behind the order.

As we create the class SimpleUser before the HelloSimpleUser, so the classpath of SimpleUser is before HelloSimpleUser? OK, I will explain the classpath order in two aspects: Java2Script Project File .j2s and Java2Script Application Configuration

Java2Script Project File .j2s

First, open the ".j2s" file under the root folder of the project (You may have to remove the .* filter to see the file: Pull down menu on the upper right corner -> Filter -> ...).

Before explaining what is inside file ".j2s", I will explain why there are a ".j2s". The ".j2s" is considered as a sign of Java2Script project. When the first time you "Enable Java2Script Compiler", the ".j2s" file will be created. And beside the ".j2s" file, there are also a "j2slib" folder with file "console.css" and "j2s-core-common.jz" is created. And the "j2slib/j2s-core-common.jz" is set to the ".j2s" file as the basic classpath for the project.

Now, let's see what's inside .j2s:

#Java2Script Configuration
#Sat Jan 14 22:08:23 CST 2006
j2s.resources.list=j2slib/j2s-core-common.jz,bin/net/sf/j2s/hello/HelloJ2SWorld.js,bin/net/sf/j2s/hello/SimpleUser.js,bin/net/sf/j2s/hello/HelloSimpleUser.js
j2s.output.path=bin
j2s.compiler.status=enable

Content in the .j2s can be modified if you understand what it means. But it's not recommended.

The resource path in the "j2s.resources.list" is added by the Java2Script compiler. Once the Java2Script is to compile a *.java file, it will check the list to see whether it's in the list. If the class represented by *.java is not in the list, *.js will be appended to the list. Because class HelloJ2SWorld, SimpleUser and HelloSimpleUser is created one by one, so the order is .../HelloJ2SWorld.js,.../SimpleUser.js,.../HelloSimpleUser.js.

Java2Script Application Configuration

As the default configuration of running Java2Script Application, the order the *.js class path will refer to the order of .j2s. But user can configure the classpath manually. Clicking the "Run ..." to have a look.

Java2Script application configuration

Switch to HelloSimpleUser under category "Java2Script Application", and swith to the tab "Classpath", you will see "Bootstrap Entries" and "User Entries" categories with some item under it.
Java2Script classpath tab
The concept of J2S classpath is similar with Java's classpath. And the similarity contains the tab of "Classpath". You may want to add or remove some new resources to classpath. You can re-order the list of those classes. Re-ordering the classes is an important concept of Java2Script Application. As some *.js files must be included before other *.js files. For example, once class User extends SexSensible, the file SexSensible.js should be included before User.js. So you may keep in mind to re-order these classes to make sure that J2S application can be run correctly.

At this time, you may want to remove the class "net.sf.j2s.hello.HelloJ2SWorld, as there are no need of HelloJ2SWorld.js for HelloSimpleUser.main method. Remove that *.js will save the launching time.

There are buttons on the right side.

And beside tab "Classpath", the tab "Main" and "Arguments" are similar with the Java's. And the arguments in "Arguments" tab is not yet implemented. And there are too new tabs "HTML" and "Console UI":

Java2Script HTML tab
Java2Script console UI tab

In tab "HTML", you can customise some HTML that will be generated into the HTML source. By this way, you can do whatever beside the J2S Application. There are 4 parts of HTML, you can take some tries. In the bottom of part "Header of Header", there are three buttons "window.alert", "document.write", "document.body.appendChild", you can generated codes to replace the build-in console "System.out.*()". This will be useful, if you replace the "j2slib/j2s-core-common.jz" with "j2slib/j2s-core-bare.js", which do not support "System.out.println" or other "System" method.

In tab "Console UI", please leave the upper two opions unchanged, as there are not activated in the version of 0.3.0. The option "Make J2S console as fast view automatically" will place the "J2S Console" view in the position of "fast view", and the option "Maximize J2S console automatically" will try to maximize the console once the J2S Application is launched. This would be useful when you are running the J2S' SWT Application.

Is it easy enough to configure J2S?

The concept of J2S Application is somewhat similar with Java Application, lots of concepts are brought from Java. So it should be easy for Java programmers. For JavaScript Programmer or Ajaxian, some concepts should be learned. But manage your codes in Eclipse, you should accept some basic Java concepts.

Java2Script Pacemaker is still improving the J2S Application mode, running J2S Application inside Eclipse would get more and more supported.

Followed articles:

Tutorial of J2S in Eclipse (3): Inheritance Example by User and Pet -- We Need This Complexity
Tutorial of J2S in Eclipse (4): How to Use java.util.* -- Resuing Java Codes
Tutorial of J2S in Eclipse (5): How to Use org.eclipse.swt.* -- Tour to UI
Tutorial of J2S in Eclipse (6): How to Use ajax.* -- A Simple RSS Reader

Resources

Down the net.sf.j2s.hello project files (zip) (61k)

Blog discussion

Have problems while reading through these articles? Or find some errors between the words? Or have some ideas on the Java2Script technology? Please make comments or discussions here in the blog.

About the author

Zhou Renjian: Initial contributor of Java2Script.