Link to home
Start Free TrialLog in
Avatar of tmar89
tmar89

asked on

Accessing helper classes for an Applet through a jar

I have an applet that is included in a jar file.  The applet class has an import for a class within package access.  The applet works in Eclipse but when I deploy it and use it with a browser, it loads the applet class and when it tries to access a helper class within the package the error console says it cannot initialize that helper class.  But if its in the jar file and it can find the applet class, why can't it find other classes in the jar?  For instance, I have package.myApplet and package.myHelperClass.  Both classes are in the jar file.  In my applet loader, I have the archive set to the jar, and the code set to package.myApplet.  It loads, but says it cannot initialize package.myHelperClass in the Java Console.

HTML:
<applet archive="myJar.jar" code="package.myApplet" height="200" width="200"></applet>

Contents of myJar.jar:
package/myApplet.class
package/myHelperClass.class

Another note is that I am trying to access the helper class statically, ie myHelperClass.someStaticMethod().  I don't create an instance.
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Please post output of

jar tf myJar.jar
Avatar of tmar89
tmar89

ASKER

META-INF/MANIFEST.MF
package/myApplet$1.class
package/myApplet$2.class
package/myApplet$3.class
package/myApplet$4.class
package/myApplet$5.class
package/myApplet$6.class
package/myApplet$7.class
package/myApplet.class
package/myHelperClass.class
Avatar of tmar89

ASKER

To give more info on the error, Java Console responds with:

Exception in thread "AWT-EventQueue-1" java.lang.NoClassDefFoundError: Could not initialize class package.myHelperClass
      at package.myApplet$1.actionPerformed(myApplet.java:42)
      at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
      at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
      at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
      at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
      at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
      at java.awt.Component.processMouseEvent(Unknown Source)
      at javax.swing.JComponent.processMouseEvent(Unknown Source)
      at java.awt.Component.processEvent(Unknown Source)
      at java.awt.Container.processEvent(Unknown Source)
      at java.awt.Component.dispatchEventImpl(Unknown Source)
      at java.awt.Container.dispatchEventImpl(Unknown Source)
      at java.awt.Component.dispatchEvent(Unknown Source)
      at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
      at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
      at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
      at java.awt.Container.dispatchEventImpl(Unknown Source)
      at java.awt.Component.dispatchEvent(Unknown Source)
      at java.awt.EventQueue.dispatchEvent(Unknown Source)
      at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
      at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
      at java.awt.EventDispatchThread.run(Unknown Source)

myApplet.java line 42:
myHelperClass.checkEvent();
Avatar of tmar89

ASKER

After rearranging some code, it turns out it is scope related.  I was calling the class from an inline method through the addActionListener method and it was not able to find the myHelperClass class.  I didn't expect this to be an issue because it works fine in the IDE but not as a standalone applet.  Here is the faulty code:

eventButton.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent evt) {
    myHelperClass.checkEvent();
  }});

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tmar89

ASKER

Yeah, that was exactly what I did.  I'll give you the credit for helping me brainstorm through this problem.
:-)