Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why Is NoClassDefFoundError Displayed Here?

 
Peter Mc Cormack
Greenhorn
Posts: 12
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using Notepad++ to write code, and command-prompt to compile and run the file on Windows 7. The problem I'm having is when testing packages I keep getting NoClassFoundError. From what I
discovered this error has something to do with a missing classpath (I think?)...The compiled class file is in the same folder as the source file.
The following is a simple class with the package folders included:

The project folder 'JavaHandCode' is on the C:\ drive. The class compiles without issue, but when I try to run the class I get the error below...

C:\javahandcode\mainpackage\package1>javac TestPackage1.java
C:\javahandcode\mainpackage\package1>java TestPackage1
Exception in thread "main" java.lang.NoClassDefFoundError: TestPackage1 (wrong
name: javahandcode/mainpackage/package1/TestPackage1)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:480)
C:\javahandcode\mainpackage\package1>


The really puzzling part is this; when I delete the package from the source code, the newly compiled file executes correctly.

Would really appreciate help with this, thanks!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your class is declared to be in package javahandcode.mainpackage.package1, and say you have directory path x/y/z as the only element of your classpath, then your class has to be in path x/y/z/javahandcode/mainpackage/package1/TestPackage1.class. You would run it as your main class by doing

I think that "wrong name" error means it found the class but not in a directory path the corresponds to its package.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess you are 3 package down the point required to run java

java -cp ../../../ javahandcode.mainpackage.package1.TestPackage1 (most probably it ill work out)

but always compile and run your java file from your root directory.
when you compile separate your package by / . and when you run use . to separate your package

 
Peter Mc Cormack
Greenhorn
Posts: 12
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, thanks for your feedback.

I'm still confused, and have attempted to follow your advice without success.
As far as I understand, the classpath and directory are the same. Both TestPackage1.java and TestPackage1.class are in the same folder. The directory folders are as follows:

C:\javahandcode\mainpackage\package1

With files TestPackage1.java and TestPackage1.class residing in folder package1.

When I compile the file I use the following command (which works fine):
C:\javahandcode\mainpackage\package1>javac TestPackage1.java

Running the following created the same NoClassDefFoundError:
C:\>java -cp ../../../ javahandcode.mainpackage.package1.TestPackage1

Running the following:
C:\>java -cp javahandcode/mainpackage/package1/ javahandcode.mainpackage.package1.TestPackage1
... created...Error: Could not find or load class javahandcode.mainpackage.package1.TestPackage1

Thanks.


 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peter Mc Cormack wrote:Hi guys, thanks for your feedback.

I'm still confused, and have attempted to follow your advice without success.
As far as I understand, the classpath and directory are the same. Both TestPackage1.java and TestPackage1.class are in the same folder. The directory folders are as follows:

C:\javahandcode\mainpackage\package1

With files TestPackage1.java and TestPackage1.class residing in folder package1.


Then:


That is, your classpath has only one element, and it is C:\ . Within that classpath, the fully qualified class name, which corresponds to the path to the class relative to a classpath root, is javahandcode.mainpackage.package1.TestPackage1.

Pay careful attention to where the spaces are.

Note that


is completely different from



and at most one of them can work for a given package and class structure.
 
Peter Mc Cormack
Greenhorn
Posts: 12
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff, thanks for the detailed response, unfortunately that classpath did not work for some reason.
However I found this article http://jarticles.com/package/package_eng.html , which instructs the setting of the classpath to:

C:\>set CLASSPATH=.;C:\;

Then I ran the class and it worked!

Again, thanks for all your help!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic