0

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?

Cheekysoft
  • 35,194
  • 20
  • 73
  • 86
JuanDeLosMuertos
  • 4,532
  • 15
  • 55
  • 87

2 Answers2

1

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
  • 114,442
  • 31
  • 189
  • 228
0

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
  • 3,248
  • 3
  • 19
  • 12
  • public Method getMethod(String name, Class>... parameterTypes) throws NoSuchMethodException, SecurityException I guess he's right in that. – alamar May 19 '09 at 08:36
  • now I get this exception: InternalError: Unable to convert JavaScript value to Java value of type java.lang.Class[] – JuanDeLosMuertos May 19 '09 at 08:36
  • 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 May 19 '09 at 08:41
  • 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[]) – JuanDeLosMuertos May 19 '09 at 08:47