0

I have a Java applet that has an int array. I want to copy this array over to a JavaScript array. Simply calling a Java function from JavaScript that returns this array results in null exception and accessing the array element by element from JavaScript leads to browser crashes. What is the correct way to handle this? Perhaps JSObject?

4
  • Java runs on the server-side. JS runs on the client side. Are you sure you want to support a call from the client to the server ? Commented Jul 24, 2013 at 6:44
  • @alfasin Since Java 1.1 its possible to run Java-Code on Client via Applets. Commented Jul 24, 2013 at 8:56
  • @PeterRader My bad - I didn't notice this question has an applet tag Commented Jul 24, 2013 at 14:11
  • @alfasin yes, often the tags are misfits (like int). Commented Jul 25, 2013 at 7:51

2 Answers 2

1

Try this:

<applet code="Applet.class" name="Applet" />
<script>var f = document.Applet.foobar();</script>

And

class Applet extends java.applet.Applet {
    public void init() {}
    public int[] foobar(){
        return new int[]{1,2,3};
    }
}
2
  • Huh, I guess that it worked now. Didn't work when I tried the same thing earlier. Thanks! Commented Jul 24, 2013 at 20:18
  • Hm, it won't work if the Browser asks for Applet-Permission first. Usually programmers write a hello-callback-function. Commented Jul 25, 2013 at 10:33
1

You could use netscape JSObject to interact with JavaScript from Java Applet. Have a look at the below tutorial. It might be useful for you.

JSObject

2
  • Thanks, but simply calling a function that returns the int array turned out to be easier. Commented Jul 24, 2013 at 20:19
  • Yes. it's simpler indeed :) Commented Jul 25, 2013 at 5:56

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.