Entry
Java: Compile: Error: Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
Apr 22nd, 2006 17:29
Knud van Eeden,
----------------------------------------------------------------------
--- Knud van Eeden --- 11 September 2003 - 02:19 pm ------------------
Java: Compile: Error: Exception in thread "main"
java.lang.NoClassDefFoundError: HelloWorld
---
This error
NoClassDefFoundError
always indicates that that class name can not be found in the CLASSPATH
===
Description:
---
1. First I could successfully compile my java program using the Java
compiler
javac.exe
2. but when I then run java.exe, I get this error:
+---------------------------------------------------------------------+
|[C:\] javac HelloWorld.java |
| |
|[C:\] java HelloWorld |
|Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld|
| |
+---------------------------------------------------------------------+
---
---
1. Possible cause: CLASSPATH variable not set.
---
Solution:
put your directory at the front of the existing classpath:
SET CLASSPATH=<your current java files directory>;%CLASSPATH%
e.g.
SET CLASSPATH=c:\temp;%CLASSPATH%
---
or similarly, put your directory at the end of the existing classpath:
SET CLASSPATH=<your current java files directory>;%CLASSPATH%
e.g.
SET CLASSPATH=c:\temp;%CLASSPATH%
---
or similarly, but note that this will overwrite your existing
classpath:
SET CLASSPATH=<your current java files directory>
---
---
Method: set the CLASSPATH environment variable:
e.g. on an MSDOS prompt type, or
add it permanently to your environment variables:
SET CLASSPATH=c:\temp;%CLASSPATH%
If you happen to have your Java files in the directory c:\temp
So all together you might do the following:
cd c:\temp
SET CLASSPATH=c:\temp;%CLASSPATH%
check the current value of your classpath by typing
SET CLASSPATH
then compile and run your program
javac.exe yourjavafilename.java
java.exe yourjavafilename
---
Note:
Possibly add the path to your javac.exe and java.exe file to your msdos
PATH environment variable)
---
---
Method: pass the classpath as a parameter to the java.exe interpreter:
compile with:
java.exe -classpath <your current java files directory> <your Java
program>
---
E.g. type on the command line:
cd c:\temp
javac.exe -classpath c:\temp Helloworld.java
java.exe -classpath c:\temp HelloWorld
to run the program 'HelloWorld.java', and with your classpath
set to 'c:\temp'.
This will possibly show:
Hello World
---
E.g. type on the command line
java.exe -classpath "." HelloWorld
The dot '.' means here the current path
===
E.g. create a batch file
--- cut here: begin --------------------------------------------------
@C:
@CD "c:\program files\test\"
@REM to test the compilation of test files and the setting of the
classpath
"C:\JDK\bin\javac.exe" -classpath ".;C:\Program
Files\test\your1.jar;C:\Program Files\test\your2.jar;C:\Program
Files\test\your3.jar;" YourJavaFileName.java
@REM run the test file
"C:\JDK\bin\java.exe" -classpath ".;C:\Program
Files\test\your1.jar;C:\Program Files\test\your2.jar;C:\Program
Files\test\your3.jar;" YourJavaFileName
--- cut here: end ----------------------------------------------------
===
2. Possible cause: You should not include any extension to your
filename when using java.exe
---
e.g.
given the filename 'myjavafilename.java', then after
compiling this, and interpreting only this works OK (as filename only
as parameter of java.exe):
---
This works OK:
java.exe myjavafilename
---
But any of this gives that error, and works so NOT ok:
---
java.exe myjavafilename.class
---
java.exe c:\temp\myjavafilename
---
java.exe c:\temp\myjavafilename.class
---
java.exe myjavafilename.<some file extension>
---
java.exe <some directory>myjavafilename.<some file extension>
===
Method: If you use 'package', check if the directory indicated by
the 'package' is correctly pointed to
E.g.
If used
package test1.test2.test3;
then this directory path should exist on your computer
(e.g. the path test1\test2\test3 should exist on your harddrive)
===
Internet: see also:
---
Java run time error messages
http://mindprod.com/jgloss/runerrormessages.html
----------------------------------------------------------------------