2

I'm cutting my teeth on some Java/JavaScript coding and I seem to have hit a wall. I'm trying to pass agruments from Java to JavaScript but no matter what I do "JSObject jso = JSObject.getWindow(this);" always throws an exception. I've done some searching and can't find any solutions. I stole the code below from a website (http://www.codejava.net/java-se/applet/liveconnect-the-api-for-communication-between-java-applet-and-javascript) and don't see any errors in either the JavaScript or the Java and both files compile correctly.

I've added plugin.jar to by buildpath and made sure that the jfxrt.jar is not in the build path. I thought something could possibly be wrong with the plugin.jar in jre7 so I tried jre6 but was getting the same error. The code I'm using is as follows.

Java Code:

package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import netscape.javascript.*;

public class TestApplet extends JApplet {

private JButton button = new JButton("Call Javascript");
private JLabel label = new JLabel();

public void init() {
    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(button, BorderLayout.NORTH);
    getContentPane().add(label, BorderLayout.SOUTH);

    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            Thread runner = new Thread(new Runnable() {
                public void run() {
                    try {
                        testLiveConnect();
                    } catch (JSException jse) {
                        // Error
                        jse.printStackTrace();
                    }
                }
            });
            runner.start();
        }
    });
}
private void testLiveConnect() throws JSException {
    JSObject jso = JSObject.getWindow(this);

    // call Javascript's method foo() with no argument
    String result = (String) jso.call("foo", null);
    label.setText(result);

    // delay 2 seconds to see the result
    try { Thread.sleep(2000); } catch (InterruptedException ie) {};

    // call Javascript's method foo() with two arguments
    result = (String) jso.call("bar", new String[] {"Alice", "Alisa"});
    label.setText(result);
    try { Thread.sleep(2000); } catch (InterruptedException ie) {};

    // execute a Javascript expression
    String expression = "alert('Hi, I am from Javascript.');";
    jso.eval(expression);
    try { Thread.sleep(2000); } catch (InterruptedException ie) {};

    // get value of a named member from Javascript
    result = (String) jso.getMember("coop");
    label.setText(result);
    try { Thread.sleep(2000); } catch (InterruptedException ie) {};

    // get value of an indexed member from Javascript
    result = (String) jso.getSlot(1);
    label.setText(result);
}
}

JavaScript Code:

<html>
    <head>
        <title>LiveConnect - Java-Javascript communnication demo</title>
    </head>
    <body>
        <center>
            <applet id="testApplet"
                code="TestApplet.class"
                width="200" height="80"
                    >
            </applet>
        </center>
    </body>
    <script type="text/javascript">
        var coop = "Ooops!";
        this[1] = "Slot 1";

        function foo() {
            return "This is from foo()";
        }

        function bar(firstName, lastName) {
            return "Greeting " + firstName + " " + lastName + "!";
        }
    </script>
</html>

Exception Thrown:

netscape.javascript.JSException
    at netscape.javascript.JSObject.getWindow(Unknown Source)
    at test.TestApplet.testLiveConnect(TestApplet.java:34)
    at test.TestApplet.access$0(TestApplet.java:33)
    at test.TestApplet$1$1.run(TestApplet.java:22)
    at java.lang.Thread.run(Unknown Source)
halfer
  • 19,824
  • 17
  • 99
  • 186
testingtester
  • 528
  • 4
  • 11

2 Answers2

3

This drove me nuts once upon a time. Java7 apparently comes with 2 jars that include different implementations of this same class. jfxrt.jar and plugin.jar

I solved issues with these by simply removing jfxrt.jar from my classpath. You'll have to dig for how to do that for your build system. In Intellij, you can go to:

File -> Project Structure -> SDKs

Then, on the classpath tab, highlight jfxrt.jar and click '-'

ETA: I found the answer that originally helped me that has a bit more info: https://stackoverflow.com/a/14156602/1057157

Community
  • 1
  • 1
Eric Haynes
  • 5,126
  • 2
  • 29
  • 36
  • Erich2k8, thanks for your response. I previously made sure that jfxrt.jar is not in my build path. I ran into that problem earlier getting my java file to compile. The exception I am experiencing has occurs at runtime. – testingtester Aug 04 '13 at 02:33
  • Hrmm. My bad. FWIW your source works fine for me in Win7 x64 in both Chrome and IE 10 – Eric Haynes Aug 04 '13 at 03:41
  • Does it work at the link below? If so, maybe it's a local applet security issue... http://eh-srv.com/applet_test/test.html – Eric Haynes Aug 04 '13 at 03:43
  • yes it works at the link you provided. I'm trying to run it in eclipse. Eclipse builds the java applet but I get the exception when I click on the button. It must be a configuration setting somewhere that I'm missing. Thanks for your help. – testingtester Aug 04 '13 at 03:55
  • 1
    When you say "run it in eclipse", does it launch a native browser to do so, or a pane within the IDE? If the former, it may be acting as a proxy to it somehow (e.g. to be a script debugger). If the latter, then it might just not be a fully featured browser that JSObject is capable of hooking in to. – Eric Haynes Aug 04 '13 at 05:09
  • It launches in a pane within the IDE. I hadn't thought of that. I'll try to run it outside eclipse and report back – testingtester Aug 04 '13 at 12:40
  • Thank you erich2k8, this was the problem. When I run it in a browser outside of Eclipse it works as expected. I thought the IDE pane would be able to handle it everything since it compiled correctly but I guess not. Thanks again. – testingtester Aug 04 '13 at 13:35
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/34797/discussion-between-erich2k8-and-ryanm2890) – Eric Haynes Aug 04 '13 at 16:20
0

I had the same problem and solved it simply by changing the following line:

JSObject jso = JSObject.getWindow(this);

to:

JSObject jso = JSObject.getWindow((Applet)this);
halfer
  • 19,824
  • 17
  • 99
  • 186
  • Good edits have been rolled back on this several times, please do not do that again - it is regarded as vandalism here. – halfer Mar 31 '17 at 15:47