Problems accessing Java classes from Firefox Javascript extension

I am going to develop a Firefox extension that uses some Java classes. The extension gets the value of the fields <input type="file">

using Javascript.

The Java class I'm going to create is the following:

public class Firefox {
    public static String inFileName;
    public static void main(String[] args) throws IOException {
        inFileName = "";
        try {
            inFileName = args[0];
        } catch (Exception ex) {}
}

      

In Javascript, I have to use Java reflection to access Java classes.

This way I can create my Java object:

var fileInput = e.explicitOriginalTarget; // is an object HTMLInputElement, I get this when a file is selected
strArray = java.lang.reflect.Array.newInstance(java.lang.Class.forName("java.net.URL"),3);
classLoader = java.net.URLClassLoader.newInstance(strArray);
parameters = java.lang.reflect.Array.newInstance(java.lang.Class.forName("java.lang.String"),1);
parameters[0]= fileInput.value;
var aClass = java.lang.Class.forName("Firefox", true, classLoader);
var aStaticMethod = aClass.getMethod("main", [parameters.getClass()]); //gets the main(String[] args) method, here I get the exception*
var myJava = aStaticMethod.invoke(null, [parameters]); //invokes the main method

      

I tried this extension on Firefox-3.5b4 and everything goes well, but when I try to use Firefox-3.0.10 I get the following exception *:

`InternalError: Unable to convert JavaScript value class [Ljava.lang.String; to Java value of type java.lang.Class[]`

      

For example, adding the following line before calling the main method:

alert(parameters + "  -  " + parameters[0]);

      

on Firefox-3.0.10 and 3.5b4 I get an alert box that says:

`[Ljava.lang.String;@194ca6c  -  /root/demo.pdf`  //demo.pdf is the selected file

      

But it's only on 3.0.10 that I get the exception, ONLY on my GNU / Linux machine. On Windows XP, I have no problem on both Firefox versions instead!

Please note that on Windows and Linux, the Java plugin version is 6 update 13.

Where am I going wrong? Is this a possible bug in Firefox-3.0.10 Javascript engine, or should I do something else before getting the main (...) method?

0


a source to share


2 answers


you are using the method incorrectly using

[parameters.getClass()]

      



which passes an argument of type java.lang.Class [] to a signature expecting a String object. just pass the parameters object as it is.

0


a source


Assuming your fully qualified class name is "your.package.Firefox" you can do:



importPackage("your.package");

args = java.lang.reflect.Array.newInstance(java.lang.String.TYPE, 1);
Firefox.main(args)

      

+1


a source







All Articles