Java in Firefox Extensions

  • Revision slug: Java_in_Firefox_Extensions
  • Revision title: Java in Firefox Extensions
  • Revision id: 69936
  • Created:
  • Creator: Brettz9
  • Is current revision? No
  • Comment 19 words added, 5 words removed

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

 /* This was necessary for a while, but not as of Java 6 update 12 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(urlArray); */ // javascript array to java array conversion does not work anymore with plugin2 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".

The extension XqUSEme borrows some of the code of the above 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. (See also the Talk page for further potential examples of class instantiations which can also work on JAR files not placed in the /components directory.

Revision Source

<p>If you are in need of calling Java code from within a Firefox <a href="/en/Extensions" title="en/Extensions">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><span class="kwd"> /* This was necessary for a while, but not as of Java 6 update 12 var</span><span class="pln"> urlClasz </span><span class="pun">=</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">lang</span><span class="pun">.</span><span class="typ">Class</span><span class="pun">.</span><span class="pln">forName</span><span class="pun">(</span><span class="str">"java.net.URL"</span><span class="pun">);</span><span class="pln"><br></span><span class="kwd">var</span><span class="pln"> urlArray </span><span class="pun">=</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">lang</span><span class="pun">.</span><span class="pln">reflect</span><span class="pun">.</span><span class="typ">Array</span><span class="pun">.</span><span class="pln">newInstance</span><span class="pun">(</span><span class="pln">urlClasz</span><span class="pun">,</span><span class="lit">1</span><span class="pun">);</span><span class="pln"><br></span><span class="pln">urlArray[0] = </span><span>new java.net.URL('http://foo.net/bar.jar'</span><span>);<br></span>var cl = new java.net.URLClassLoader(<span class="pln">urlArray); */  </span><strike><span class="pln">// </span>javascript array to java array conversion does not work anymore with plugin2 </strike>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> </p>
<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>
<p>The extension <a class="link-https" href="https://addons.mozilla.org/en-US/firefox/addon/5515">XqUSEme</a> borrows some of the code of the above 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. (See also the Talk page for further potential examples of class instantiations which can also work on JAR files not placed in the /components directory.</p>
Revert to this revision