6 Replies Latest reply: Dec 21, 2007 12:50 PM by 807601 RSS

    Java runtime issuses NoClassDefFoundError

    807600
      I am new to java. So to learn how to use java I have been going through the learning trails. The trail I am currently reading is: Creating a GUI with JFC/Swing. I having problems complying and running one of the first examples: HelloWorldSwing.java. -----

      After it complies I get this error:

      C:\JAVA>java HelloWorldSwing
      Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldSwing (wrong name: start/HelloWorldSwing)
      at java.lang.ClassLoader.defineClass1(Native Method)
      at java.lang.ClassLoader.defineClass(Unknown Source)
      at java.security.SecureClassLoader.defineClass(Unknown Source)
      at java.net.URLClassLoader.defineClass(Unknown Source)
      at java.net.URLClassLoader.access$000(Unknown Source)
      at java.net.URLClassLoader$1.run(Unknown Source)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
      at java.lang.ClassLoader.loadClass(Unknown Source)
      at java.lang.ClassLoader.loadClassInternal(Unknown Source)

      Any help in getting this code to run would be appreciated.
        • 1. Re: Java runtime issuses NoClassDefFoundError
          796447
          Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldSwing (wrong name: start/HelloWorldSwing)
          So does your class belong to the "start" package? As in, did you do this at the top of your source file:
          package start;
          If so, then your class name is start.HelloWorldSwing , not just HelloWorldSwing. So tell the VM to run that class name. Also you'll need to ensure it is compiled in the right directory matching the package name.
          • 2. Re: Java runtime issuses NoClassDefFoundError
            807600
            Thanks warnerja
            • 3. Re: Java runtime issuses NoClassDefFoundError
              807600
              Thanks! It would be nice if they said that in the tutorial.
              • 4. Re: Java runtime issuses NoClassDefFoundError
                807601
                The HelloWorldSwing.java file provided by the Swing tutorial (http://java.sun.com/docs/books/tutorial/uiswing/start/compile.html) does have "package start;" at the beginning of the code. But when I compile (javac HelloWorldSwing.java) the .class files go into the same directory as the .java file. I found I needed to manually create a "start" directory and copy the .class files into it before I could run the program (java start.HelloWorldSwing).

                My question is, is there a problem with the tutorial, since it simply says to run "java HelloWorldSwing"?

                And another question, how do I get the compiler to create the the package directory structure, which I would have assumed it would do automatically?

                Thank you for your help!
                • 5. Re: Java runtime issuses NoClassDefFoundError
                  796447
                  jrn36 wrote:
                  The HelloWorldSwing.java file provided by the Swing tutorial (http://java.sun.com/docs/books/tutorial/uiswing/start/compile.html) does have "package start;" at the beginning of the code. But when I compile (javac HelloWorldSwing.java) the .class files go into the same directory as the .java file. I found I needed to manually create a "start" directory and copy the .class files into it before I could run the program (java start.HelloWorldSwing).

                  My question is, is there a problem with the tutorial, since it simply says to run "java HelloWorldSwing"?

                  And another question, how do I get the compiler to create the the package directory structure, which I would have assumed it would do automatically?

                  Thank you for your help!
                  Yes the tutorial is slightly erroneous.

                  To answer the question about making it create the proper package directory, use this command:
                  javac -d . HelloWorldSwing.java
                  That would end up creating the "start" subdirectory off the current directory. Then, as long as you don't have a CLASSPATH environment variable set up (which you should not), when you type:
                  java start.HelloWorldSwing
                  from that same directory (the parent of "start"), it should work.
                  • 6. Re: Java runtime issuses NoClassDefFoundError
                    807601
                    Awesome, just what I needed to know! Thanks!