3 Replies Latest reply on Apr 22, 2007 9:24 PM by 796365

    java.lang.NoClassDefFoundError in Applet

    807599
      When I'm trying to run a MainApplet.class applet I get this exception:
      My html file :
      <APPLET
      CODE="MainApplet.class"
      WIDTH="100%" HEIGHT="90"
      >
      </APPLET>


      MainApplet :
      package common;

      import java.awt.Graphics;

      import javax.swing.JApplet;
      import javax.swing.JFrame;

      public class MainApplet extends JApplet {
      public void init() {}
      ...
      ...

      Html file and applet are at the common directory.


      java.lang.NoClassDefFoundError: MainApplet (wrong name: common/MainApplet)
           at java.lang.ClassLoader.defineClass1(Native Method)
           at java.lang.ClassLoader.defineClass(Unknown Source)
           at java.security.SecureClassLoader.defineClass(Unknown Source)
           at sun.applet.AppletClassLoader.findClass(Unknown Source)
           at java.lang.ClassLoader.loadClass(Unknown Source)
           at sun.applet.AppletClassLoader.loadClass(Unknown Source)
           at java.lang.ClassLoader.loadClass(Unknown Source)
           at sun.applet.AppletClassLoader.loadCode(Unknown Source)
           at sun.applet.AppletPanel.createApplet(Unknown Source)
           at sun.plugin.AppletViewer.createApplet(Unknown Source)
           at sun.applet.AppletPanel.runLoader(Unknown Source)
           at sun.applet.AppletPanel.run(Unknown Source)
           at java.lang.Thread.run(Unknown Source)
      Exception in thread "Thread-80" java.lang.NullPointerException
           at sun.plugin.util.GrayBoxPainter.showLoadingError(Unknown Source)
           at sun.plugin.AppletViewer.showAppletException(Unknown Source)
           at sun.applet.AppletPanel.runLoader(Unknown Source)
           at sun.applet.AppletPanel.run(Unknown Source)
           at java.lang.Thread.run(Unknown Source)

      So, what's the problem?

      Please help.
        • 1. Re: java.lang.NoClassDefFoundError in Applet
          807599
          Move your html file one step down in your directory tree, so
          that your project dir looks like this

          /project/applet.html
          /project/common/MainApplet.class

          Then change the html file like this

          <APPLET
          CODE="common.MainApplet.class"
          WIDTH="100%" HEIGHT="90"
          >
          </APPLET>

          I believe this should do it.

          kari-matti
          • 2. Re: java.lang.NoClassDefFoundError in Applet
            807599
            Hi,

            I had a similar problem. After moving the httml file one step down in the directory tree, the error is gone. However, I still have a problem to load the JDBC driver from Applet, but the same driver loads fine from JFrame.

            appletviewer -J-Djava.security.policy=test.policy TestApplet.html

            I have set following permission in test.policy file.
            grant {
            permission java.security.AllPermission;
            };

            Then, I get following error.
            ----
            java.lang.ClassNotFoundException: org.gjt.mm.mysql.Driver
            at sun.applet.AppletClassLoader.findClass(AppletClassLoader.java:168)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
            at sun.applet.AppletClassLoader.loadClass(AppletClassLoader.java:119)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
            at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
            at java.lang.Class.forName0(Native Method)
            at java.lang.Class.forName(Class.java:164)
            ----

            I have loaded the driver as below.
            ----
            try {
            Class.forName("org.gjt.mm.mysql.Driver");
            }catch(Exception e){e.printStackTrace();}

            DriverManager.getConnection(url, username, password);

            ---

            I am pretty sure that I have set the CLASSPATH variable correctly, otherwise it would NOT have worked from JFrame.

            Does anybody out there know why it doesn't work from Applet although it works from Frame?

            Thanks in advance.
            • 3. Re: java.lang.NoClassDefFoundError in Applet
              796365
              Use the fully-qualified path to the driver - I believe the applet is performing as per the following tutorial sentence:

              Whenever an applet needs to load some data from a file that's specified with a relative URL (a URL that doesn't completely specify the file's location), the applet usually uses either the code base or the document base to form the complete URL.

              Alternatively, you could try setting the code/document base.

              (and next time start your own thread, please)