简体   繁体   English

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

[英]Trouble accessing Java classes from Firefox extension's Javascript

I'm going to develop a Firefox extension which uses some Java classes. 我将开发一个使用某些Java类的Firefox扩展。 The extension gets the value of <input type="file"> fields, using Javascript. 该扩展使用Javascript获取<input type="file">字段的值。

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方法调用之前:

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. 但是只有在3.0.10上,我才例外,仅在我的GNU / Linux机器上。 On Windows XP instead I have no problems on both Firefox versions! 相反,在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(...)方法之前我必须做任何其他事情?

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)

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. 这会将类型为java.lang.Class []的参数传递给需要String对象的签名。 simply pass the parameters object in as it is. 只需按原样传递参数对象即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM