JSObject.getWindow(本);总是抛出异常

时间:2013-08-04 01:35:02

标签: java javascript jsobject

我正在削减一些Java / JavaScript编码,我似乎已经碰壁了。我试图将Java中的agruments传递给JavaScript,但不管我做什么“JSObject jso = JSObject.getWindow(this);”总是抛出异常。我做了一些搜索,找不到任何解决方案。我从网站(http://www.codejava.net/java-se/applet/liveconnect-the-api-for-communication-between-java-applet-and-javascript)中窃取了下面的代码,并且没有看到JavaScript或Java中的任何错误,并且两个文件都正确编译。

我已经通过buildpath添加了plugin.jar,并确保jfxrt.jar不在构建路径中。我认为jre7中的plugin.jar可能有问题,所以我尝试了jre6但是得到了同样的错误。我正在使用的代码如下。

Java代码:

package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import netscape.javascript.*;

public class TestApplet extends JApplet {

private JButton button = new JButton("Call Javascript");
private JLabel label = new JLabel();

public void init() {
    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(button, BorderLayout.NORTH);
    getContentPane().add(label, BorderLayout.SOUTH);

    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            Thread runner = new Thread(new Runnable() {
                public void run() {
                    try {
                        testLiveConnect();
                    } catch (JSException jse) {
                        // Error
                        jse.printStackTrace();
                    }
                }
            });
            runner.start();
        }
    });
}
private void testLiveConnect() throws JSException {
    JSObject jso = JSObject.getWindow(this);

    // call Javascript's method foo() with no argument
    String result = (String) jso.call("foo", null);
    label.setText(result);

    // delay 2 seconds to see the result
    try { Thread.sleep(2000); } catch (InterruptedException ie) {};

    // call Javascript's method foo() with two arguments
    result = (String) jso.call("bar", new String[] {"Alice", "Alisa"});
    label.setText(result);
    try { Thread.sleep(2000); } catch (InterruptedException ie) {};

    // execute a Javascript expression
    String expression = "alert('Hi, I am from Javascript.');";
    jso.eval(expression);
    try { Thread.sleep(2000); } catch (InterruptedException ie) {};

    // get value of a named member from Javascript
    result = (String) jso.getMember("coop");
    label.setText(result);
    try { Thread.sleep(2000); } catch (InterruptedException ie) {};

    // get value of an indexed member from Javascript
    result = (String) jso.getSlot(1);
    label.setText(result);
}
}

JavaScript代码:

<html>
    <head>
        <title>LiveConnect - Java-Javascript communnication demo</title>
    </head>
    <body>
        <center>
            <applet id="testApplet"
                code="TestApplet.class"
                width="200" height="80"
                    >
            </applet>
        </center>
    </body>
    <script type="text/javascript">
        var coop = "Ooops!";
        this[1] = "Slot 1";

        function foo() {
            return "This is from foo()";
        }

        function bar(firstName, lastName) {
            return "Greeting " + firstName + " " + lastName + "!";
        }
    </script>
</html>

异常抛出:

netscape.javascript.JSException
    at netscape.javascript.JSObject.getWindow(Unknown Source)
    at test.TestApplet.testLiveConnect(TestApplet.java:34)
    at test.TestApplet.access$0(TestApplet.java:33)
    at test.TestApplet$1$1.run(TestApplet.java:22)
    at java.lang.Thread.run(Unknown Source)

2 个答案:

答案 0 :(得分:3)

这曾经让我疯狂。 Java7显然带有2个包含同一类的不同实现的jar。 jfxrt.jar和plugin.jar

我通过从类路径中删除jfxrt.jar来解决这些问题。您必须挖掘如何为您的构建系统执行此操作。在Intellij中,您可以访问:

档案 - &gt;项目结构 - &gt;的SDK

然后,在类路径选项卡上,突出显示jfxrt.jar并单击&#39; - &#39;

ETA:我找到了原来对我有帮助的答案:https://stackoverflow.com/a/14156602/1057157

答案 1 :(得分:-1)

我遇到了同样的问题,只需更改以下行即可解决问题:

JSObject jso = JSObject.getWindow(this);

为:

JSObject jso = JSObject.getWindow((Applet)this);