views:

647

answers:

2

I'm going to develop a Firefox extension which uses some Java classes. The extension gets the value of <input type="file"> fields, 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) {}
}

On Javascript, I have to use Java reflection in order to access Java classes.

In this manner 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've been trying this extension on Firefox-3.5b4 and everything goes fine, but when I try it on 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, putting the following line before the main method invokation:

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

on both Firefox-3.0.10 and 3.5b4 I get an alert window which says:

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

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

Note that on both Windows and Linux the Java plugin version is 6 update 13.

Where am I wrong? Is it a possible bug on Firefox-3.0.10 Javascript engine or must I do any other thing before getting the main(...) method?

A: 

you are incorrectly invoiking the method using;

[parameters.getClass()]

which is passing an argument of type java.lang.Class[] into the signature that is expecting a String object. simply pass the parameters object in as it is.

simon622
public Method getMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityExceptionI guess he's right in that.
alamar
now I get this exception: InternalError: Unable to convert JavaScript value to Java value of type java.lang.Class[]
Giancarlo
try changing the two reflecting lines to;var aStaticMethod = aClass.getMethod("main", parameters.getClass()); //gets the main(String[] args) method, here I get the exception*var myJava = aStaticMethod.invoke(null, parameters);removing the [] initializers since they already seem to have been created by the reflection call.
simon622
new error: There is no Java method java.lang.Class.getMethod that matches JavaScript argument types (string, object).Candidate methods with the same name are: java.lang.reflect.Method getMethod(java.lang.String, java.lang.Class[])
Giancarlo
+1  A: 

assuming your complete class name is "your.package.Firefox" you could do:

importPackage("your.package");

args = java.lang.reflect.Array.newInstance(java.lang.String.TYPE, 1);
Firefox.main(args)
dfa
how do I set the "args" value?
Giancarlo
ReferenceError: importPackage is not defined
Giancarlo
in "plain" rhino this is supposed to work sorry :(
dfa
Hi, just following-up DFA's comment: Related info:: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/LiveConnect_Overview/JavaScript_to_Java_Communication
ATorras
I've already tried out all stuff from developer.mozilla.org, with no success...
Giancarlo