9 Replies Latest reply on Jan 10, 2003 7:19 PM by 807549

    wrong name: NoClassDefFoundError

    807549
      I've been searching some old posts on this topic, because I have this error when I try to run my program with java HtmlAdder:

      Exception in thread "main" java.lang.NoClassDefFoundError: HtmlAdder (wrong name
      : com/erikprice/HtmlAdder/HtmlAdder)
      at java.lang.ClassLoader.defineClass0(Native Method)
      at java.lang.ClassLoader.defineClass(ClassLoader.java:502)
      at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
      3)
      at java.net.URLClassLoader.defineClass(URLClassLoader.java:250)
      at java.net.URLClassLoader.access$100(URLClassLoader.java:54)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:193)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
      at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)


      I'm in the same directory as the file "HtmlAdder.class", and it is a part of the package "com.erikprice.HtmlAdder". It compiles fine, but when it runs, I get that error.

      Anyone know what I am doing wrong?
        • 1. Re: wrong name: NoClassDefFoundError
          807549
          try

          java com.erikprice.HtmlAdder.HtmlAdder
          • 2. Re: wrong name: NoClassDefFoundError
            807549
            Thanks Tolmank, but I still get an error (not the same ones I guess):

            D:\User Profiles\eprice\My Documents\java\devel\myjava\ com\erikprice\HtmlAdder> java com.erikprice.HtmlAdder.HtmlAdder

            Exception in thread "main" java.lang.NoClassDefFoundError: com/erikprice/HtmlAdder/HtmlAdder



            The strange thing is that I know this class exists, it's right there. I just compiled it. And there is a public static void main method to let me execute it.

            Any ideas?
            • 3. Re: wrong name: NoClassDefFoundError
              807549
              Go to the directory
              D:\User Profiles\eprice\My Documents\java\devel\myjava\

              and then execute the java command as spefied by Tolmank.
              • 4. Re: wrong name: NoClassDefFoundError
                807549
                Thanks gerritcap, that worked. Can you tell me why it doesn't work when I'm deep inside the package's directory structure? I have the deep directory in my CLASSPATH too, so I would have thought it would work.
                • 5. Re: wrong name: NoClassDefFoundError
                  jschellSomeoneStoleMyAlias
                  Thanks Tolmank, but I still get an error (not the same
                  ones I guess):

                  D:\User Profiles\eprice\My
                  Documents\java\devel\myjava\ com\erikprice\HtmlAdder>
                  java com.erikprice.HtmlAdder.HtmlAdder

                  Exception in thread "main"
                  java.lang.NoClassDefFoundError:
                  com/erikprice/HtmlAdder/HtmlAdder

                  The strange thing is that I know this class exists,
                  it's right there. I just compiled it. And there is a
                  public static void main method to let me execute it.
                  It has nothing to do with main. It doesn't matter whether main exists or not.

                  Java can not find the file. The most likely reason is that your class path is wrong. The next most likely reason is because the name is different - names are case sensitive.
                  • 6. Re: wrong name: NoClassDefFoundError
                    DrClap
                    If your class is in a package, then it is necessary for the package to be in the classpath, not the class. As gerritcap implied.
                    • 7. Re: wrong name: NoClassDefFoundError
                      807549
                      Its with packages

                      If you launch
                      java mypackage.mysubpackage.MyClass
                      it is going to look in the classpath directories for a file with intermediate directories mypackage/mysubpackage/MyClass.class
                      One of the things a classloader checks regarding integrity of a .class file is that that particular .class file really describes a class of the mypackage.mysubpackage package.

                      Hence if you launch java MyClass from the deep directory you basically instruct the java vm to launch the main method of the class MyClass which is supposed to be in the "top level" package and then the classloader notices that the definition of that class inside the class file indicates it is not part of the top level package but of mypackage.mysubpackage. Hence the error

                      Typically I put .java files in a src file somewhere and then compile with the -d option to another bin directory and that bin directory is in my CLASSPATH variable.
                      • 8. Re: wrong name: NoClassDefFoundError
                        807549
                        Thanks, that explains it for me. It's my first time playing with my own packages. (that sounds funny doesn't it)
                        Typically I put .java files in a src file somewhere
                        and then compile with the -d option to another bin
                        directory and that bin directory is in my CLASSPATH
                        variable.
                        That's a good idea. I'll remember that.
                        • 9. Re: wrong name: NoClassDefFoundError
                          807549
                          Hi everyone --

                          I've got the same problem, but I'm using the IDE from Sun. When I hit the play button in SunONE Studio 4, I get this same error. I have confirmed that the class file is in the same directory that bears the name of the package -- it had better, since I used the IDE to compile! Does anyone have any other ideas about how to solve this? Thanks!