From MozillaZine Knowledge Base
Here's an example from a blog: http://mozilla-firefox-extension-dev.blogspot.com/2004/11/calling-java-code-in-custom-jars-from.html
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);
Calling showJavaConsole is very useful for debugging:
var jvm = Components.classes["@mozilla.org/oji/jvm-mgr;1"].getService(Components.interfaces.nsIJVMManager); jvm.showJavaConsole();
Here's the code I use to log exceptions. JSUtil.getStackTrace doesn't always work. I haven't figured out why, so I do it last, wrap it in a try catch, and print out my own "rough" stack trace first. (The rough list has no line numbers.) Wrap all java calls in a try catch and log the exception. Mozilla doesn't always report them and when it does it doesn't give the stack trace, and it's usually wrapped in a meaningless exception. I think the java console logs all exceptions though, so maybe this isn't necessary if you use that.