The following program is throwing error:
public class HelloWorld {
public static void main(String args[]) {
System.out.println(\"Hello World!\");
I spent four hours trying various permutations & search suggestions.
At last, found this post that worked but not the best solution to change the original code to test it.
set classpath=JDKbin;JDKlib;JREbin;JRElib;myClassLib;.
java <packageName>.<className>
also tried the java ..\<packageName>.<className>
Neither worked.
However first response alone worked. Thank you so much Abhi!!!
"I found one another common reason. If you create the java file inside a package using IDE like eclipse, you will find the package name on the top of your java file like "package pkgName". If you try to run this file from command prompt, you will get the NoClassDefFoundError error. Remove the package name from the java file and use the commands in the command prompt. Wasted 3 hours for this. -- Abhi"
Curious if there is any other way to make it work without having to change the original code? Thank you!
The javadoc of NoClassDefFounError itself would be a good start (here), and then I'll suggest you clean and rebuild your project.
I finally found this as a bug with Apache Netbeans editor:
Below steps will remove the error:
Hope that helps, if you are using new Apache Netbeans (not old Netbeans)
Run it like this:
java -jar HelloWorld.jar
package com.beans;
public class Flower{
.......
}
packages :=> com.beans,
java class Name:=> Flower.java,
Folder structure (assuming):=> C:\com\beans\Flower.*(both .java/.class exist here)
then there are two ways of executing it:
1. Goto top Folder (here its C:\>),
then : C:> java com.beans.Flower
2. Executing from innermost folder "beans" here (C:\com\beans:>),
then: C:\com\beans:> java -cp ./../.. com.beans.Flower
Exception in thread "main" java.lang.NoClassDefFoundError
One of the places java tries to find your .class
file is your current directory. So if your .class
file is in C:\java
, you should change your current directory to that.
To change your directory, type the following command at the prompt and press Enter:
cd c:\java
This .
tells java that your classpath is your local directory.
Executing your program using this command should correct the problem:
java -classpath . HelloWorld