gpt4 book ai didi

java - JSObject.getWindow(this);总是抛出异常

转载 作者:行者123 更新时间:2023-12-02 06:50:53 26 4
gpt4 key购买 nike

我正在尝试一些 Java/JavaScript 编码,但似乎遇到了困难。我试图将参数从 Java 传递到 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 中没有看到任何错误,并且两个文件都编译正确。

我已将plugin.jar 添加到buildpath 中,并确保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)

最佳答案

这曾经让我发疯。 Java7 显然附带了 2 个 jar,其中包含同一类的不同实现。 jfxrt.jar和plugin.jar

我通过简单地从类路径中删除 jfxrt.jar 解决了这些问题。您必须深入研究如何为您的构建系统做到这一点。在Intellij中,您可以访问:

文件 -> 项目结构 -> SDK

然后,在类路径选项卡上,突出显示 jfxrt.jar 并单击“-”

预计到达时间:我找到了最初对我有帮助的答案,其中包含更多信息:https://stackoverflow.com/a/14156602/1057157

关于java - JSObject.getWindow(this);总是抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18038951/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com