Dev : Extensions : Example Code : Calling Java from Javascript: Difference between revisions

From MozillaZine Knowledge Base
Jump to navigationJump to search
No edit summary
(No difference)

Revision as of 23:17, 26 October 2005

This page is currently notes of what I had to do to call Java from Javascript.

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);


In order to write to the file system, I had to add this to the javascript code: java.lang.System.setSecurityManager(null); I could read a jar from the filesystem without a problem.

Calling showJavaConsole is very useful for debugging:

        var jvm = Components.classes["@mozilla.org/oji/jvm-mgr;1"].getService(Components.interfaces.nsIJVMManager);
	jvm.showJavaConsole();

http://www.xulplanet.com/references/xpcomref/ifaces/nsIJVMManager.html#method_showJavaConsole