Revision 138792 of Java in Firefox Extensions

  • Revision slug: Talk:Java_in_Firefox_Extensions
  • Revision title: Java in Firefox Extensions
  • Revision id: 138792
  • Created:
  • Creator: Brettz9
  • Is current revision? No
  • Comment clarification re: security policy

Revision Content

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...
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.

I also needed a .java.policy file added to the user home directory to give privileges to my jars. (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.

Revision Source

<p>it might be useful to add something like:
</p><p>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
</p>
<hr>
<p>It'd also nice, if we had an actually working example... :|
</p>
<dl><dd> I'd like to second that...
</dd></dl>
<p><br>
</p>
<dl><dd> 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... </dd></dl>
<code><pre>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
</pre></code>
<p>A more complex situation arises, however, when one needs a class to have its constructor invoked. This can be done using the following pattern.
</p>
<code><pre>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.
</pre></code>
<p>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 <a class=" external" href="http://java.sun.com/j2se/1.4.2/docs/api/java/lang/package-tree.html" rel="freelink">http://java.sun.com/j2se/1.4.2/docs/...kage-tree.html</a> for allowable types).
</p><p>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.
</p><p><s>I also needed a .java.policy file added to the user home directory to give privileges to my jars.</s> (I found I could also use the code and technique used in the <a href="en/Java_Firefox_Extension">Java Firefox Extension</a> 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.
</p>
Revert to this revision