C:\Users\Eric\Desktop>javac HelloWorld.java
//HelloWorld.class is created on my desktop
C:\Users\Eric\Desktop>java HelloWorld.class
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld/class
Caused by: java.lang.ClassNotFoundException: HelloWorld.class
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)
Could not find the main class: HelloWorld.class. Program will exit.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
SCJP 6: 96% SCWCD 5: 98%
marc weber wrote:Welcome to the ranch!
When you run java from the command line, supply the class name without the ".class" extension. Just type...
java HelloWorld
zameel amjed wrote
if you're still getting this no class def found error, try running in this way: java -cp . HelloWorld
Rajat Jindal wrote:I hope you got the solution for your problem but still I would like to add something which is some times more important in debugging such problems.
Even if you try to run code using
java <fileName>
and you get NoClassDefFoundException error, then first thing you should check is your system classpath. In case your system classpath is not pointing to current directory then it may give you an error.
You can check classpath in DOS using command
set classpath
and you can set the classpath as
set classpath=%classpath%;.;
Rajat Jindal wrote:1. <fileName> meant to be fully qualified file name
2. You cant escape yourself by setting classpath on command line, in many situations you always have a classpath defined in your system environmental variables, so better way is to just make a check.
Jeff Verdegan wrote
Class name, not file name. They are two different things.
The command line one will override the environment variable. Different apps need different classpaths, and some installers (QuickTime) mess with your CLASSPATH environment variable.
Disagree. If you set a classpath environment variable for every possible application, that environment variable will expand and become too large for you to handle.Rajat Jindal wrote: . . . So I would say its better to set it in environmental variable . . .
No more fooling around. Read this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|