Java in Firefox Extensions

  • Revision slug: Java_in_Firefox_Extensions
  • Revision title: Java in Firefox Extensions
  • Revision id: 69933
  • Created:
  • Creator: Jtwjguevara
  • Is current revision? No
  • Comment placed full package name in the java.lang.Class.forName() method.

Revision Content

If you are in need of calling Java code from within a Firefox extension, you can make use of LiveConnect. LiveConnect gives your extension's JavaScript code (linked from or contained in XUL code) access to 2 objects: java and Packages. 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, e.g.,

var cl = new java.net.URLClassLoader(
    [ new java.net.URL('http://foo.net/bar.jar') ]
);

var aClass = java.lang.Class.forName("org.mozilla.developer.HelloWorld", true, cl);
var aStaticMethod = aClass.getMethod("getGreeting", []);
var greeting = aStaticMethod.invoke(null, []);
alert(greeting);

This technique only works in JavaScript code linked from or contained in XUL files. If you wish to call Java code from within JavaScript code that implements some XPCOM components, at this time, you need a different technique (refer to the Java Firefox Extension). A good reason for calling Java from within an XPCOM component rather than from XUL is to maintain a singleton (some singular Java object) across all Firefox windows. If you call Java from XUL, then each Firefox window will have its own class loader and hence its own "singleton".

Revision Source

<p>
If you are in need of calling Java code from within a Firefox <a href="en/Extension">extension</a>, you can make use of <a class="external" href="http://en.wikipedia.org/wiki/LiveConnect">LiveConnect</a>. LiveConnect gives your extension's JavaScript code (linked from or contained in XUL code) access to 2 objects: <code>java</code> and <code>Packages</code>. These 2 objects let you make use of the standard JDK classes, e.g.,
</p>
<pre class="eval">var aJavaList = new java.util.LinkedList();
</pre>
<p>If you want to load your own JARs, then you can create your own Java class loader, e.g.,
</p>
<pre class="eval">var cl = new java.net.URLClassLoader(
    [ new java.net.URL('http://foo.net/bar.jar') ]
);

var aClass = java.lang.Class.forName("org.mozilla.developer.HelloWorld", true, cl);
var aStaticMethod = aClass.getMethod("getGreeting", []);
var greeting = aStaticMethod.invoke(null, []);
alert(greeting);
</pre>
<p>This technique only works in JavaScript code linked from or contained in XUL files. If you wish to call Java code from within JavaScript code that implements some XPCOM components, at this time, you need a different technique (refer to the <a class="external" href="http://simile.mit.edu/java-firefox-extension/">Java Firefox Extension</a>). A good reason for calling Java from within an XPCOM component rather than from XUL is to maintain a singleton (some singular Java object) across all Firefox windows. If you call Java from XUL, then each Firefox window will have its own class loader and hence its own "singleton".
</p>
Revert to this revision