We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS

Java in Firefox Extensions

it might be useful to add something like:

if you get the message "java is not defined", make sure to enable java in your browser preferences; this is also required for running the extension lateron, so you might want to add this to your extension description


It'd also nice, if we had an actually working example... :|

I'd like to second that...


Ok, while the example in the article didn't work for me (or I couldn't figure out how to get it to work right), the following pattern does...


This was necessary for a time, but not as of Java 6 update 12 which fixed many formerly broken LiveConnect issues
// java script array to java array conversion does not work with the new plugin2. //var cl = new java.net.URLClassLoader( var urlClasz = java.lang.Class.forName("java.net.URL");
var urlArray = java.lang.reflect.Array.newInstance(urlClasz,1);
urlArray[0] = new java.net.URL('http://foo.net/bar.jar');
*/ var cl = new java.net.URLClassLoader(
    [ new java.net.URL('http://foo.net/bar.jar') ] ); var loadedClass = c1.loadClass("SomeClass"); var classInst = loadedClass.newInstance(); classInst.someMethod(); // One can then invoke methods (including passing in variables which get type converted automatically

A more complex situation arises, however, when one needs a class to have its constructor invoked. This can be done using the following pattern.


var reflect = java.lang.reflect;
var paramtypes = reflect.Array.newInstance(java.lang.Class, 1); // Prepare an array of size 1 to hold the java type "Class"; an array is expected, so if you only need to pass in one item type, just make the array of size 1
paramtypes[0] = someClassToPass; // This class can be created in the manner of the code in the example above
var constructor = xmlmanClass.getConstructor(paramtypes);
var arglist = reflect.Array.newInstance(java.lang.Object, 1); // Now we can prepare to pass in an object based on the type of class just above
arglist[0] = someObjectToPassIn; // add some object (as created with a method such as in the above code example) 
var newObj = constructor.newInstance(arglist); // Finally we can pass the arguments into the constructor, returning an object, etc.

The above example uses an object (with its type determined by its class); to pass in another type, such as an integer, specify the java integer type first, and then pass in your specific integer: see http://java.sun.com/j2se/1.4.2/docs/...kage-tree.html for allowable types).

Since the try-catch exceptions didn't work for me in a number of cases (though the documentation here states that try-catch in Javascript should catch Java exceptions), I was forced to make my own class in Java (and load it in the URL Class Loader too) so that I could catch exceptions within that file.

<strike>I also needed a .java.policy file added to the user home directory to give privileges to my jars.</strike> (I found I could also use the code and technique used in the Java Firefox Extension with its firefoxClassLoader JAR, being sure to load this JAR and my own only once at the startup of the extension) (To get DLL's to work, I needed to add them to the /components directory with an .autoreg file or to the Program Files\Mozilla Firefox files. When files were created, they were added to that folder as well.

Document Tags and Contributors

Contributors to this page: Brettz9, ignisvulpis, Mnyromyr, Rhiow
Last updated by: Brettz9,