Re: "Proper" way to Access Java from javascript
Brett Zamir <[email protected]> Mon, 03 Dec 2007 05:55:39 +0800
| Newsgroups | gmane.comp.mozilla.devel.java |
|---|---|
| Message-ID | <475329DB.1060004__35835.9271464453$1196632596$gmane$org@yahoo.com> |
sam keen wrote:
> liveconnect:
> - is this true "LiveConnect may be removed from Mozilla 2.0 according
> to this post by Josh."
>
I'd sure like an answer on this one too....
> - for statements like var red = new Packages.HelloWorld(); where do
> you place your jar file so it can be found?
>
>
I believe that Packages will not allow you access to your own packages
(I learned this the hard way). Try using URLClassLoader. See
http://developer.mozilla.org/en/docs/Talk:Java_in_Firefox_Extensions for
examples of usage with LiveConnect... If the files are local you can use
the file:/// protocol... I needed to put the loader (called via
LiveConnect) in global scope for my own extension that uses this, so
that it would not attempt to load multiple times and thus cause errors
(I guess with DLL files)...
And for my case of finding my own extension directory to find the jar
file, I did, with the bold portion at the end being my own extension
identifier:
var path =
Components.classes["@mozilla.org/file/directory_service;1"].getService(
Components.interfaces.nsIProperties).get("ProfD",
Components.interfaces.nsIFile).path; // Get path to user profile folder
var extpath = path+'/extensions/*[email protected]/'*;
One problem with this though is that "Classes loaded by a URLClassLoader
have whatever permissions are granted to their java.security.CodeSource
by the system java.security.Policy" (Java in a Nutshell, p. 582). You
need to either have permission granted to the jars in a user home
.java.policy file, or use the trick (a good trick) in the Java Firefox
extension to get permissions granted for your app. I'm doing the former
now during testing, but hope to use the Java Firefox extension (XPCOM)
approach in the future (which also gives the benefit of getting one
single Java object for all windows)...
One other tip: the try-catch blocks in Javascript do not work for me--I
needed to perform them within Java (I built wrapper methods to make this
expandable).
best wishes,
Brett