Forums Register Login

Why Is NoClassDefFoundError Displayed Here?

+Pie Number of slices to send: Send
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!
1
+Pie Number of slices to send: Send
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.
1
+Pie Number of slices to send: Send
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

+Pie Number of slices to send: Send
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.


1
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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!
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1111 times.
Similar Threads
Package Issue / Class Path issue
Compiling a class which is dependent on other class
package access trouble
Running java with a classpath on cygwin
Exception in thread "main" java.lang.NoClassDefFoundError: Voice1
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 20:06:08.