使用deployJava.js在javascript中调用java方法

时间:2012-03-30 14:40:59

标签: java javascript html5 applet

我想在javascript中调用java方法,Andrew Thompson建议使用deployJava.js库。我按照这些说明操作: http://docs.oracle.com/javase/6/docs/technotes/guides/jweb/deployment_advice.html

这里解释了如何在javascript中使用java类,但我想从javascript中调用java方法。 (这是因为我想在java中导入.owl文件,将json格式的信息导出到用javascript编写的代码中。) 有人知道如何使用deployJava库吗?

这是我导入java文件的代码:

<noscript>A browser with JavaScript enabled is required for this page to operate properly.</noscript>
    <h1>Sending Messages to Other Applets</h1>
    <script>
        function sendMsgToIncrementCounter() {
        receiver.incrementCounter();
    }
</script>

<p>Sender Applet</p>
<script>
    var attributes = { id:'sender', code:'Sender.class', width:300, height:50} ;
    var parameters = {} ;
    deployJava.runApplet(attributes, parameters, '1.6');
</script>
<br/>
<br/>
<p>Receiver Applet</p>
<script>
    var attributes = { id:'receiver', code:'../Receiver.class', width:300, height:50} ;
    var parameters = {} ;
    deployJava.runApplet(attributes, parameters, '1.6');
</script>

这是发件人和收件人的java文件:

import javax.swing.*;

public class Receiver extends JApplet {
    private int ctr = 0;
    private JLabel  ctrLbl = null;

    public void init() {
        //Execute a job on the event-dispatching thread; creating this applet's GUI.
        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {             
                    ctrLbl = new JLabel("");
                    add(ctrLbl);
                }
            });
        } catch (Exception e) {
            System.err.println("Could not create applet's GUI");
        }
    }

    public void incrementCounter() {
        ctr++;
        String text = " Current Value Of Counter: " + (new Integer(ctr)).toString();
        ctrLbl.setText(text);
    }
}

import javax.swing.*;

import java.awt.event。; import netscape.javascript。;

public class Sender extends JApplet实现了ActionListener {

public void init() {
    //Execute a job on the event-dispatching thread; creating this applet's GUI.
    try {
        final ActionListener al = this;
        SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
                JButton btn = new JButton("Click To Increment Counter");
                add(btn);
                btn.addActionListener(al);
            }
        });
    } catch (Exception e) {
        System.err.println("createGUI didn't complete successfully");
    }
}

public void actionPerformed(ActionEvent e) {
    try {
        JSObject window = JSObject.getWindow(this);
        window.eval("sendMsgToIncrementCounter()");
    } catch (JSException jse) {
        jse.printStackTrace();
    }
}

}

我只是从本网站上给出的示例中复制粘贴: http://docs.oracle.com/javase/tutorial/deployment/applet/iac.html 这个例子在我的浏览器中运行得很好,所以它的完成方式是正确的,但我怀疑我没有正确导入javafiles,因为这是来自je java-console的错误:

load: class Sender.class not found.
java.lang.ClassNotFoundException: Sender.class
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Applet2ClassLoader.java:195)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass0(Plugin2ClassLoader.java:249)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Plugin2ClassLoader.java:179)
    at sun.plugin2.applet.Plugin2ClassLoader.loadClass(Plugin2ClassLoader.java:160)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at sun.plugin2.applet.Plugin2ClassLoader.loadCode(Plugin2ClassLoader.java:690)
    at sun.plugin2.applet.Plugin2Manager.createApplet(Plugin2Manager.java:3045)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Plugin2Manager.java:1497)
    at java.lang.Thread.run(Thread.java:680)
Exception: java.lang.ClassNotFoundException: Sender.class

3 个答案:

答案 0 :(得分:1)

将原始方法与新JS片段相结合,并在上一个问题(调整后)上接受部分答案,给出了..

<html>
<head>
<script>
// dangerous to have a 0x0 applet!  Some security plug-ins regard it 
// as suspicious & automatically remove the element.  Better to set it
// not visible using styles
var attributes = {
    codebase:'../sesame',
    code:'applet_test',
    width:10, 
    height:10
};
var parameters = {fontSize:16} ;
var version = '1.6' ;
deployJava.runApplet(attributes, parameters, version);

function test() {
    var app = document.applet_test;
    alert("Screen Dimension\r\n  width:" + app.getScreenWidth()
        + " height:" + app.getScreenHeight());
}
</script>
<body>

<FORM>
<INPUT 
    type="button" 
    value="call JAVA"
    onClick = "test()">
</FORM>

<script>
deployJava.runApplet(attributes, parameters, version);
</script>

</body>
</html>

但我只是把它写在了我的头顶。不要相信我,相信validation service。 ;)

答案 1 :(得分:1)

我建议您设置一个简单的网络服务,您的javascript代码可以使用。它并不需要非常积极,我个人使用JAX-RS的简单REST布局(jersey非常适合使用),特别是如果你想要一些简单的JSON支持内置(使用正确的插件)。

尝试与页面上的小程序实际通信可能,但非常依赖浏览器和恕我直言不值得麻烦。如果您正在网络上工作,也可以使用网络服务。

答案 2 :(得分:0)

属性中给出的.class文件的目录存在问题。这是正确的代码:

<p>Sender Applet</p>
<script>
    var attributes = { id:'sender', code:'sesame/Sender.class', archive:'sesame/applet_SenderReceiver.jar', width:300, height:50} ;
    var parameters = {} ;
    deployJava.runApplet(attributes, parameters, '1.6');
</script>
<br/>
<br/>
<p>Receiver Applet</p>
<script>
    var attributes = { id:'receiver', code:'sesame/Receiver.class', archive:'sesame/applet_SenderReceiver.jar', width:300, height:50} ;
    var parameters = {} ;
    deployJava.runApplet(attributes, parameters, '1.6');
</script>