Mozilla + Firefox extension development

Thursday, November 04, 2004

Calling Java code in custom JARs from Javascript

This is perhaps the easiest way to call Java code in your own custom JAR files from Javascript code in your extension:


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

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


If you would rather include your JAR file in your extension package, you might need to map a chrome URL to a file path, which can be done using jslib.

Please beware that the code above might freeze the browser for a short while as the JVM loads.

7 Comments:

  • Neat!
    Any more code samples you have worked on?

    By Anonymous Anonymous, at 5:04 PM  

  • Where is 'Packages' defined??

    By Anonymous Anonymous, at 7:41 AM  

  • Does not work with thunderbird. It throws errors as "packages not defined" error.

    Tried to get Rhino scripting working, but doesnot seem to happen.

    By Anonymous Anonymous, at 2:45 PM  

  • Would you be interested in how you can build a new custom-designed house at a 42% to 100% discount?

    By Blogger www.Business-Loans.ws, at 6:48 PM  

  • Is this meant to work with Thunderbird as well? It doesn't recognize "packages." What do I need to do to set this up? Any examples of distributed Java extensions?

    (Please use email address on website)

    By Anonymous Mark Dredze, at 4:47 PM  

  • How would you go about bundling an applet into an XPI and accessing it?

    You imply a means via jslib?

    High-level is OK. I've tried a few things and run into difficulties with the chrome URL.

    By Blogger NullCorpus, at 9:10 PM  

  • Please see http://simile.mit.edu/java-firefox-extension/

    By Blogger David Huynh, at 9:15 PM  

Post a Comment

<< Home