Note: Out-of-process Java, enabled by default on all platforms but Windows, currently breaks this code in the Firefox 4.0 betas; see bug 563891. Since bug 563891 is designated as a soft blocker, the functionality upon which the below code depends may or may not be available in the final Firefox 4.0 release.
Packages (note that per this thread, although the new documentation for the LiveConnect reimplementation states that these globals will be deprecated (in the context of applets), "Firefox and the Java Plug-In will continue to support the global java/Packages keywords, in particular in the context of Firefox extensions."). These 2 objects let you make use of the standard JDK classes, e.g.,
var aJavaList = new java.util.LinkedList();
If you want to load your own JARs, then you can create your own Java class loader.
The following approach is taken from the extension XqUSEme (note you must use the latest version, currently still in the sandbox, as prior versions only worked with LiveConnect before Java 6 update 11) which borrows some of the code of the Java Firefox Extension in order to grant full privileges to Java within a Firefox extension, but it is easier to understand and doesn't require creation of a XPCOM component. [NOTE: A simpler Hello World version than that below is now available at http://brett-zamir.me/java_demo/ ]
For privileges, the basic procedure is:
- Download and include http://simile.mit.edu/repository/jav...nsionUtils.jar within your extension (you can also build your own from the source files at http://simile.mit.edu/repository/jav...xtensionUtils/ )
- Build and add references leading to this JAR and all other JARs in your extension within an array of java.net.URL's, pass to java.net.URLClassLoader to get a class loader, and finally pass the classloader and array to a function which gives the necessary privileges:
Now you can begin using LiveConnect, including referencing the classes in your JARs. Be aware that if you have added a namespace within your classes (via "package"), you will need to reference the namespace as well.
var aClass = java.lang.Class.forName("org.mozilla.developer.HelloWorld", true, cl); var aStaticMethod = aClass.getMethod("getGreeting", ); var greeting = aStaticMethod.invoke(null, ); alert(greeting);
Another, perhaps simpler approach is as follows:
var myClass = loader.loadClass('com.example.myClass'); // use the same loader from above var myObj = myClass.newInstance(); var binval = myObj.myMethod(arg1, arg2); // Pass whatever arguments you need (they'll be auto-converted to Java form, taking into account the LiveConnect conversion rules)
For more complex cases, in which you need to call a specific constructor with arguments, you will need reflection. The following is a somewhat simplified snippet from XqUSEme:
var reflect = java.lang.reflect; // Build an array of the class types which are expected by our constructor (in this case, java.io.File and a class from another JAR we loaded, com.sleepycat.db.EnvironmentConfig) var paramtypes = reflect.Array.newInstance(java.lang.Class, 2); // 2nd argument should indicate the number of items in following array paramtypes = java.io.File; var envconfigClass = loader.loadClass('com.sleepycat.db.EnvironmentConfig'); paramtypes = envconfigClass; // Get the constructor var constructor = envClass.getConstructor(paramtypes); // Now that we have the constructor with the right parameter types, we can build the specific arguments we wish to pass to it var arglist = reflect.Array.newInstance(java.lang.Object, 2); // 2nd argument should indicate the number of items in the following array var mydir = new java.io.File(dirURL); // a file URL arglist = mydir; var envconfig = envconfigClass.newInstance(); arglist = envconfig; // Call our constructor with our arguments var env = constructor.newInstance(arglist);
- LiveConnect Overview