使用 LiveConnect 的 Javascript 到 Java 通信不起作用

发布于 2024-10-31 12:00:43 字数 1913 浏览 1 评论 0原文

我一直在从事一个需要 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怎言笑 2024-11-07 12:00:43

我弄清楚问题到底出在哪里。原来我使用的 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!

我的痛♀有谁懂 2024-11-07 12:00:43
alert("Call some java:" + document.getElementbyId("theApplet").testFunc());

确保将 testFunc() 方法声明为 public 访问权限。

如果这不起作用,请将小程序代码发布为 SSCCE

顺便说一句

不正确

code="com/company/MyApplet.class" 

正确

code="com.company.MyApplet" 

顺便说一句2

不正确

..scriptable="yes">

正确

..scriptable="true">
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">
审判长 2024-11-07 12:00:43

由于 applet 元素已被弃用,我使用以下代码,该代码至少在 Firefox 中有效:

<object id="MyApplet" classid="java:com.example.myapplet"
  codetype="application/java" codebase="bin/" height="10" width="10"
</object>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文