Javascript to Java communication using LiveConnect not working
I've been working on a project that requires communication both directions between Java and JavaScript. I have successfully managed to get it working under all browsers in OS X, but I'm now faced with the challenge of gett开发者_高级运维ing it to run on Windows under any browser. At the moment it simply doesn't work.
I'm just wondering if there is something special I need to do in order for JavaScript to communicate with Java?
My applet code looks like this:
<applet id='theApplet'
code="com/company/MyApplet.class"
archive="SMyApplet.jar"
height="50" width="900"
mayscript="true" scriptable="yes">
Your browser is ignoring the applet tag.
</applet>
Once the applet has loaded, I then try to call functions on it like this:
alert("Call some java:" + theApplet.testFunc());
And in the firebug console I get the following error:
theApplet.testFunc is not a function
I can confirm that this doesn't work in IE either.
When the page loads, I have the java console open and I can see that the applet is successfully loading and ready to accept calls.
Any help would be greatly appreciated!
Cheers
Update: Here is the stripped down java code exposing the public api that I'm trying to call.
package com.company;
import com.google.gson.Gson;
import java.applet.*;
import java.io.*;
import java.net.*;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.*;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.*;
import netscape.javascript.*;
public class MyApplet extends Applet implements Runnable
{
public void init()
{
JSON = new Gson();
isReadyVar = 0;
workThread = null;
}
public void start()
{
}
public void run()
{
System.out.println("Done");
}
public void stop()
{
}
public void destroy()
{
}
/* Public API */
public int testFunc()
{
return 200;
}
}
Update [SOLVED]:
I figured out what the problem was exactly. Turns out the Gson lib I was using wasn't signed; but my own jar was. Browsers on windows require that all libs are signed; so I packaged Gson in with my java files & signed the lot and it solved the problem! Thanks for everyones help!
I figured out what the problem was exactly. Turns out the Gson lib I was using wasn't signed; but my own jar was. Browsers on windows require that all libs are signed; so I packaged Gson in with my java files & signed the lot and it solved the problem! Thanks for everyones help!
alert("Call some java:" + document.getElementbyId("theApplet").testFunc());
Make sure the testFunc()
method is declared as public
access.
If that does not work, post the applet code as an SSCCE.
BTW
Incorrect
code="com/company/MyApplet.class"
Correct
code="com.company.MyApplet"
BTW 2
Incorrect
..scriptable="yes">
Correct
..scriptable="true">
Since the applet
element is deprecated, I use following code, which works at least in Firefox:
<object id="MyApplet" classid="java:com.example.myapplet"
codetype="application/java" codebase="bin/" height="10" width="10"
</object>
精彩评论