When you run a J2s application in eclipse with menu Run->Run as..->Java2Script Application
the Java2Script plugin generates a simple html document that execute the java application. For doing that
it is required to perform at least the following tasks:
load the j2slib.z.js Java2Script runtime script
configure J2S runtime
Load main class java class dependencies
execute some ava code like calling the main() method of main class.
You can use the generated html file in yout web applications, nevertheless, it is very common to launch your Java2Script application from an existing
html document, or from a server page (php, jsp asp, whatever).
In those cases, it is
very helpful to understand how J2s applcations are loaded and executed. So lets examine a minimal
html document that calls our HelloWorld java example. For generating the following html markup, the HelloWorkd java class
is launched with a clean configuration:
A short version of generated HTML document is the following:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head><title>my.first.project.HelloWorld</title>
<script type="text/javascript" src="../path/to/j2slib/j2slib.z.js"></script>
</head>
<body>
<script type="text/javascript">
ClazzLoader.packageClasspath ("java", "../path/to/j2slib/", true);
ClazzLoader.setPrimaryFolder ("bin");
ClazzLoader.packageClasspath (["my.first.project", "net.sf.j2s.probes"], "bin/");
ClazzLoader.loadClass ("my.first.project.HelloWorld", function () {
my.first.project.HelloWorld.main([]);
});
</script>
</body>
</html>
|
-
First of all it is required to include the script j2slib.z.js.
-
With this call to ClazzLoader.packageClasspath we tell the java VM where are main
java sources like java.lang, java.util, etc. classes, usually at the same location of file j2slib.z.js.
-
Calling ClazzLoader.setPrimaryFolder indicates to the java VM what is the primary
folder. In the case of a java package collition, the primary folder has precedence.
-
Exactly the same way we have done for java main sources, this call tells the VM where to find the
classes of our project, and that is at the "bin" folder.
-
Now the intersting part. At this point we are ready to invoke some java code from our html document. We have already tell the
VM where are the sources of each java package we need.
Remember that in Java2Script will lazily load all java classes (load only when needed). So, for executing some java code that
involves some java classes, we first need to load
some java classes and only when loaded, we can do our java. In this case, we need to invike HelloWorld.main(), and so, we
ask to load the class my.first.project.HelloWorld and only when it is loaded (in the function callback, second parameter),
we invoke our java code my.first.project.HelloWorld.main([]);.
-
As we said in the previous item, this is the actual java code that will be invoked. Note that the java sintax is very straightfordward.
Also notice that in this case we only need to call main method, but nothing prevent us for doing more complex things, like:
var requiredClasses = ["my.first.project.HelloWorld",
"new java.util.HashMap", "java.util.Arrays"];
ClazzLoader.loadClass (requiredClasses, function () {
var arr = ["hello", "world", "1", "two"];
var map1 = new java.util.HashMap ();
for (var i = 0; i < arr.length; i++) {
map1.put (new Integer (arr[i].length), arr[i]);
}
java.util.Arrays.sort(arr);
});
|
As we can see, the ClazzLoader
javascript object contains an API do dialogate with the java runtime from javascript. It lets us configure
and manipulate the "java virtual machine", for
example, telling where main java library code is located, set the "class path", dynamically loading java
classes and give as a context from where to call java classes
We will examine and document each of this functions in the Section called Java Runtime Emulation API in Appendix A.