我无法让 Java Applet 与托管该 Applet 的页面上的 Javascript 代码进行通信。有时它会起作用,但有时它会抛出一个模糊的异常,除了一些从未解决的 Java bug 报告之外,谷歌搜索没有发现任何有用的信息(感谢 Sun)。
这是我正在使用的代码:
JSObject win = JSObject.getWindow(this);
Object[] args = new Object[1];
args[0] = "test argument";
String result = (String) win.call("testJSfunc", args); // XXX
这是我在标记为 // XXX 的行上遇到的异常。请注意,它是间歇性的。通常它可以工作,但有时却不能,使用相同的代码。重复重新加载页面很快就会产生错误。
netscape.javascript.JSException: No registered plugin for applet ID 1
at sun.plugin2.main.client.MessagePassingJSObject.newJSException(Unknown Source)
at sun.plugin2.main.client.MessagePassingJSObject.waitForReply(Unknown Source)
at sun.plugin2.main.client.MessagePassingJSObject.call(Unknown Source)
at TestApplet.testCallJS(TestApplet.java:159)
at TestApplet.init(TestApplet.java:139)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
我应该使用另一种更稳定的 Java 和 Javascript 之间的通信方式吗?
--- 更多信息 ---
我正在使用 Sun 提供的部署Java.js 脚本来动态嵌入小程序。我确保 MAYSCRIPT 已启用。我知道这不是 MAYSCRIPT 的问题,因为通信有时可以正常工作。
I'm having trouble getting a Java Applet to communicate with the Javascript code on the page the applet is hosted on. It works sometimes, but othertimes it throws an obscure exception, that googling for has not turned up any useful information, besides a few Java bug reports that were never resolved (thanks Sun).
Here is the code I am using:
JSObject win = JSObject.getWindow(this);
Object[] args = new Object[1];
args[0] = "test argument";
String result = (String) win.call("testJSfunc", args); // XXX
Here is the exception I get on the line marked // XXX. Note that it is intermittent. Often it works, but sometimes it does not, using the same exact code. Reloading the page repeatedly will produce the error pretty quickly.
netscape.javascript.JSException: No registered plugin for applet ID 1
at sun.plugin2.main.client.MessagePassingJSObject.newJSException(Unknown Source)
at sun.plugin2.main.client.MessagePassingJSObject.waitForReply(Unknown Source)
at sun.plugin2.main.client.MessagePassingJSObject.call(Unknown Source)
at TestApplet.testCallJS(TestApplet.java:159)
at TestApplet.init(TestApplet.java:139)
at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Is there another more stable way of communicating between Java and Javascript that I should be using?
--- More info ---
I'm using the deployJava.js script as provided by Sun to embed the applet on the fly. I made sure MAYSCRIPT is enabled. I know it's not a problem with MAYSCRIPT, because the communication works some of the time.
发布评论
评论(3)
确保您的小程序标签中有 MAYSCRIPT。
Make sure you have MAYSCRIPT in your applet tag.
我想我已经通过设置
classloader_cache=false
和separate_jvm=true
解决了这个问题......一旦我设置了这两个,间歇性的模糊错误就不再发生。希望这能永久解决问题。I think I've solved the problem by setting
classloader_cache=false
andseparate_jvm=true
...once I set both of those, the intermittent obscure errors stopped happening. Hopefully this solves the problem for good.如果将separate_jvm 设置为true,这会对性能产生非常糟糕的影响,因为新的Java VM 会为每个applet 实例化。如果页面上有多个小程序(30+),则速度相当慢。
我想知道这个问题是否还有其他解决方案。例如,我们有多个跨多个选项卡的小程序。当第一个带有小程序的选项卡打开时,所有小程序都会正确显示,没有任何问题。但是,切换到任何其他选项卡结果时都会出现“没有注册插件”的错误。在 IE 中,不会发生此错误,因为在 IE 中,所有小程序在添加到 DOM 时都会立即实例化。
If you set separate_jvm to true this has very bad effect on performance since new Java VM is instantiated for every applet. If you have multiple applets on the page (30+) this is pretty slow.
I wonder if there is any other solution for this issue. E.g., we have multiple applets across multiple tabs. When the first tab with applets is opened all applets are shown correctly with no problems. But, switch to any other tab results with the error for no registered plugin. In IE this error does not occur because in IE all applets are instantiated immediately as they are added to DOM.