views:

590

answers:

2

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.

+1  A: 

Make sure you have MAYSCRIPT in your applet tag.

<applet id="..." code="..." name=".." codebase="..." archive="..."  width="100%" height="100%" MAYSCRIPT>
Zoidberg
Yes, it's there. MAYSCRIPT shouldn't be the problem anyway, because the exception is intermittent. Some times it works, some times it does not (simply reloading the page, not changing any code). If MAYSCRIPT were the problem, it would block the communication 100% of the time.
davr
I think MAYSCRIPT is ignored in PlugIn2. It never worked across all browsers correctly anyway.
Tom Hawtin - tackline
Googled 'plugin2', interesting, wonder if their new plugin architecture introduced in java 1.6.10 has some bugs.
davr
'plugin2' also defines a new way of interacting with the browser. Maybe I should use that, maybe it is more stable?
davr
My bad...the new way is for DOM manipulation only. You use the exact same method for calling javascript (via the JSObject class). Internally it has been rewritten, but you use the same API.
davr
A: 

I think I've solved the problem by setting classloader_cache=false and separate_jvm=true...once I set both of those, the intermittent obscure errors stopped happening. Hopefully this solves the problem for good.

davr