• Post Reply Bookmark Topic Watch Topic
  • New Topic

NoClassDefFoundError pergatory  RSS feed

 
Brad Strausbaugh
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to write, compile and run a simple "Hello World" Java program. It compiles fine but when I try to run it I get a NoClassDefFoundError.

My j2sdk resides at the top level directory on the C drive - ie c:\j2sdk1.4.2_12\bin, and my JAVA_HOME is set to c:\j2sdk1.4.2_12. My path and classpath are both set to c:\j2sdk1.4.2_12\bin. In my DOS shell it all looks like this ...

C>
C>set path
Path=C:\WINDOWS\system32;C:\WINDOWS;C:\System32\Wbem;C:\j2sdk1.4.2_12\bin;C:\j2sdkee1.3.1\bin;C:\Sun\SDK\bin
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH

C>set classpath
CLASSPATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;c:\j2sdk1.4.2_12\bin

C>set JAVA_HOME
JAVA_HOME=C:\j2sdk1.4.2_12

C>type Test.java

public class Test {
public static void main(String[] args) {
System.out.print("Hello World!");
}
}

C>javac Test.java

C>java Test
Exception in thread "main" java.lang.NoClassDefFoundError: Test

C>

I am completely at a loss. Does anything obvious jump out?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. CLASSPATH should, ideally not be set at all. The default if no value is set is just "the current directory", whatever that may be, and that's generally the best setting.

Later, if you decide you really need it set for some reason, then always include a "." entry. The period or "dot" stands for, again, the current directory.

Or, you can tell the java.exe program to ignore CLASSPATH and use the class path you specify in the command line with the -cp switch:

java -cp . Test

(that's java space dash cp space dot space Test).
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
C>set classpath
CLASSPATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;c:\j2sdk1.4.2_12\bin

Your CLASSPATH is set wrong. The CLASSPATH is where Java looks for *.class files. The Windows system directories and the bin directory of the JDK do not belong in the CLASSPATH - those directories do not contain *.class files that Java uses.

So, remove those directories from the CLASSPATH. As Ernest says, it's best not to set the CLASSPATH at all. If you do set the CLASSPATH, add "." to it, which means "the current directory". For example:

set CLASSPATH=C:\myproject\classes;.

See How To Set The Classpath in the FAQ.
 
Brad Strausbaugh
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks much, Ernest and Jesper. That's just what I needed.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!