0

I want to call java methods in javascript and Andrew Thompson suggested to use the deployJava.js library for this. I followed these instructions: http://docs.oracle.com/javase/6/docs/technotes/guides/jweb/deployment_advice.html

Here is explained how to use the java class in javascript, but I would like to call the java methods from within the javascript. (This is because I want to import a .owl file in java en export the information in json-format to my code written in javascript.) Does anybody know how to do this with the deployJava library?

This is my code to import the java file:

<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>

and this is are the sender and receiver java files:

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 implements 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();
    }
}

}

I just copy-paste this from the example given on this site: http://docs.oracle.com/javase/tutorial/deployment/applet/iac.html This example works perfect in my browser, so the way it is done is correct, but I suspect that I don't import the javafiles correct, since this are the errors from 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
2
  • As an aside. Is this a server/client as suggested by the class names? If so, look into the 3 methods in AppletContext that have 'stream' in the name. They allow applets in the same JRE to communicate directly with each other. Commented Mar 31, 2012 at 11:03
  • Also, if you have discovered the answer, please enter it as an 'Answer' & accept it. Commented Mar 31, 2012 at 11:04

3 Answers 3

1

Combining your original method, with the new JS snippet, and part of the accepted answer on your last question (tweaked), gives..

<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>

But I just wrote that up off the top of my head. Don't trust me, trust a validation service. ;)

Sign up to request clarification or add additional context in comments.

2 Comments

I think that the problem with this is that there isn't a connection between de test-procedure (it gets the object applet_test) and the applet deployed at the bottom: deployJava.runApplet(attributes, parameters, version); I updated my information in the question.
I have got it working now, like I thought it was a problem with the directory of the files themselves. I changed added the solution to my answer, thank you for all the help
1

I would advise setting up a simple webservice that your javascript code can use. It doesn't need to be very involved, personally I'd use a simple REST layout with JAX-RS (jersey is really nice to work with), especially if you want something simple with JSON support built-in (with the right plugin).

Trying to actually communicate with the applet on the page might be possible, but very browser dependent and IMHO not worth the hassle. If you're working on the web, might as well use a web service.

Comments

0

There was a problem with the directory of the .class files given in the attributes. Here is the correct code:

<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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.