views:

1982

answers:

1

I started to ask this question and then figured out the answer before submitting it. I've decided to post the question anyway so that other people who run into the same problem will be able to learn from my mistakes.


I'm having a problem with an applet (a JApplet actually) unable to instantiate another class which is included in the same jar as the applet. The exception I'm seeing on the Java console is:

Exception in thread "thread applet-com.company.program.cm.hmi.MediatorApplet-1" java.lang.NoClassDefFoundError: com/company/program/cm/cs/JDataStore
    at com.company.program.cm.hmi.MediatorApplet.getMediator(MediatorApplet.java:63)
    at com.company.program.cm.hmi.MediatorApplet.init(MediatorApplet.java:49)
    at sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.company.program.cm.cs.JDataStore
    at sun.plugin2.applet.Applet2ClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    ... 4 more
Caused by: java.io.IOException: open HTTP connection failed:http://localhost:8080/TransportHMI/pages/com/company/program/cm/cs/JDataStore.class
    at sun.plugin2.applet.Applet2ClassLoader.getBytes(Unknown Source)
    at sun.plugin2.applet.Applet2ClassLoader.access$000(Unknown Source)
    at sun.plugin2.applet.Applet2ClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    ... 8 more

I know that the JDataStore class is included in the jar. If I list the contents using jar tvf CM_Library.jar I can see that it is there under the proper package. However, the chained exceptions above lead me to believe that the class loader isn't searching the archive for JDataStore, and is looking for the JDataStore.class file on the web server instead. Why is this? The class loader knows to load the MediatorApplet class from the jar, why doesn't it check it for JDataStore as well? In case I haven't specified the parameters correctly on the applet tag, I'll include that here as well:

<applet id="mediator-applet"
        width="0"
        height="0"
        codebase="./"
        archive="CM_Library.jar"
        code="com.company.program.cm.hmi.MediatorApplet">
</applet>
+5  A: 

Found the answer from looking at a suggestion posted for a related question. Eddie's answer didn't solve that particular problem, but it did give me the solution for mine.

What isn't particularly obvious from my question is that the JDataStore class inherits from another class which is contained in a different jar. I hadn't had to deal with the implementation details of JDataStore in a couple of months, so I completely forgot that its parent class, org.json.simple.JSONObject, wasn't in CM_Library.jar, but was in json_simple-1.0.2.jar. The fix is fairly simple, just copy the missing jar to the codebase directory and add the missing jar to the comma-separated list of archives in the applet tag's archive attribute:

<applet id="mediator-applet"
        width="0"
        height="0"
        codebase="./"
        archive="CM_Library.jar, json_simple-1.0.2.jar"
        code="com.company.program.cm.hmi.MediatorApplet">
</applet>

That fixes the error. The exception message is not particularly helpful. It would lead you to believe that it can't find the class at all, when the actual problem is that it cannot load the super class for the requested class.

A. Levy
thanks for this - I spent hours trying to figure this out.
Matt
Good to know this question was helpful to someone other than me.
A. Levy