无法从Firefox扩展的Javascript访问Java类

时间:2022-09-25 12:24:37

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.

我将开发一个使用一些Java类的Firefox扩展。扩展使用Javascript获取字段的值。

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

我要创建的Java类如下:

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.

在Javascript上,我必须使用Java反射才能访问Java类。

In this manner I can create my Java object:

通过这种方式,我可以创建我的Java对象:

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*:

我一直在Firefox-3.5b4上尝试这个扩展,一切都很顺利,但是当我在Firefox-3.0.10上尝试它时,我得到以下异常*:

`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:

例如,在main方法invokation之前放置以下行:

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

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

在Firefox-3.0.10和3.5b4上我都有一个警告窗口,上面写着:

`[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!

但只有在3.0.10我才能获得例外,仅在我的GNU / Linux机器上。在Windows XP上,我在两个Firefox版本上都没有问题!

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

请注意,在Windows和Linux上,Java插件版本都是6更新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?

我哪里错了?它是Firefox-3.0.10 Javascript引擎上的一个可能的错误,还是在获取main(...)方法之前我必须做任何其他事情?

2 个解决方案

#1


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.

它将java.lang.Class []类型的参数传递给期望String对象的签名。只需按原样传递参数对象即可。

#2


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

假设您的完整类名是“your.package.Firefox”,您可以这样做:

importPackage("your.package");

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

#1


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.

它将java.lang.Class []类型的参数传递给期望String对象的签名。只需按原样传递参数对象即可。

#2


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

假设您的完整类名是“your.package.Firefox”,您可以这样做:

importPackage("your.package");

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