Invoke Java code from Javascript

Standard Java Classes To use the standard Java classes from Javascript, there are only 2 steps:

  1. Insert Packages in front of the fully qualified name of the Java class that you would like to use.
  2. Assign that fully qualified class name to a local variable.

Here is simple example illustrating the 2 steps:

var myJsVariable = new Packages.java.util.ArrayList();    


Another more complex example:

// Javascipt    Packages.fully.qualified.java.class.name.
var ArrayList = Packages.java.util.ArrayList;
var jArray = new ArrayList(); // Creating ArrayList object.
 
jArray.add(1); // Using add() of ArrayList object.
jArray.add(2);
jArray.add(3);
 
// Using Java's static System.out.println() method.
var jOut = Packages.java.lang.System.out;
for(var i = 0; i<jArray.size(); i++)
{
  jOut.println(jArray.get(i));
}    

If the code above is executed, then the output will be displayed in the Java Console of your Firefox located at Tools->Java Console.