使用 LiveConnect 的 Javascript 到 Java 通信不起作用
我一直在从事一个需要 Java 和 JavaScript 之间双向通信的项目。我已经成功地让它在 OS X 中的所有浏览器下运行,但我现在面临着让它在 Windows 上的任何浏览器下运行的挑战。目前它根本不起作用。
我只是想知道是否需要做一些特殊的事情才能让 JavaScript 与 Java 进行通信?
我的小程序代码如下所示:
<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>
一旦小程序加载完毕,我就会尝试像这样调用它的函数:
alert("Call some java:" + theApplet.testFunc());
在 firebug 控制台中,我收到以下错误:
theApplet.testFunc is not a function
我可以确认这在 IE 中也不起作用。
当页面加载时,我打开了 java 控制台,我可以看到小程序已成功加载并准备好接受调用。
任何帮助将不胜感激!
干杯
更新:这是精简的 java 代码,公开了我试图调用的公共 api。
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;
}
}
更新[已解决]:
我弄清楚问题到底是什么。原来我使用的 Gson lib 没有签名;但我自己的罐子是。 Windows 上的浏览器要求所有库都经过签名;所以我将 Gson 与我的 java 文件一起打包;签了签,问题就解决了!感谢大家的帮助!
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 getting 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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我弄清楚问题到底出在哪里。原来我使用的 Gson lib 没有签名;但我自己的罐子是。 Windows 上的浏览器要求所有库都经过签名;所以我将 Gson 与我的 java 文件一起打包;签了签,问题就解决了!感谢大家的帮助!
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!
确保将
testFunc()
方法声明为public
访问权限。如果这不起作用,请将小程序代码发布为 SSCCE。
顺便说一句
不正确
正确
顺便说一句2
不正确
正确
Make sure the
testFunc()
method is declared aspublic
access.If that does not work, post the applet code as an SSCCE.
BTW
Incorrect
Correct
BTW 2
Incorrect
Correct
由于
applet
元素已被弃用,我使用以下代码,该代码至少在 Firefox 中有效:Since the
applet
element is deprecated, I use following code, which works at least in Firefox: