2

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!

Ben Novakovic
  • 581
  • 7
  • 17
  • @Ben "Here is the stripped down java code.." Who cares? What I suggested (and will closely look at) is an **SSCCE.** If you have not followed the link to the SSCCE, I suggest you do so now. – Andrew Thompson Apr 12 '11 at 01:49
  • ok sure; I'll create a SSCCE ver now. – Ben Novakovic Apr 12 '11 at 01:56
  • Ok, I have managed to get a working version - I just removed all the excess code and now I have a signed applet + test html page that operates as expected. Will now build back up until I recreate the problem. Thanks for the help Andrew; I just needed that push to strip it back to make sure it was actually working at the lowest level. – Ben Novakovic Apr 12 '11 at 02:34
  • No, I understand and I followed your steps to creating such an env which prove to me something in the code was breaking it under windows. I haven't updated the code above as yet; if thats what you are basing your last comment on? – Ben Novakovic Apr 12 '11 at 04:25
  • It was - silly me. :p Add a comment when you get that SSCCE posted. – Andrew Thompson Apr 12 '11 at 05:58
  • Are there JRE version differences between your OSX machine and your Windows one? – abarax Apr 12 '11 at 07:01

3 Answers3

1

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!

Ben Novakovic
  • 581
  • 7
  • 17
0
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">
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Thanks for the response. That didn't work either; have tried before too with no avail. Will post applet code shortly. – Ben Novakovic Apr 11 '11 at 04:21
  • Just before I paste the code; given that it works on OSX in FF/Chrome/Safari; shouldn't the Java code be correct as it stands? – Ben Novakovic Apr 12 '11 at 00:34
  • `File f = new File("the\\path\\to\\file.txt")` might work on Windows but fail on other OS'. So while Java itself can be x-plat, there are still many pitfalls that Java code can fall into. And besides, in this case you are mixing Java, JavaScript, HTML and a browser. That adds an order of magnitude more chance of 'incompatibility' to creep in - most of which is beyond Java's ability to control. – Andrew Thompson Apr 12 '11 at 01:44
0

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>
RoToRa
  • 37,635
  • 12
  • 69
  • 105
  • I was under the impression (and I read somewhere) that the object method was no longer recommended. I will have a hunt around for the document. – Ben Novakovic Apr 12 '11 at 00:33
  • Sorry, can't hep you then. Could you maybe put an example online? That may help. – RoToRa Apr 12 '11 at 13:10