aspose file tools
The moose likes Beginning Java and the fly likes NoClassDefFoundError pergatory Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "NoClassDefFoundError pergatory" Watch "NoClassDefFoundError pergatory" New topic
Author

NoClassDefFoundError pergatory

Brad Strausbaugh
Greenhorn

Joined: Feb 25, 2007
Posts: 21
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
Marshal

Joined: Jul 08, 2003
Posts: 24061
    
  13

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).


[Jess in Action][AskingGoodQuestions]
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12929
    
    3

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Brad Strausbaugh
Greenhorn

Joined: Feb 25, 2007
Posts: 21
Thanks much, Ernest and Jesper. That's just what I needed.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: NoClassDefFoundError pergatory
 
Similar Threads
NoClassDefFoundError
Eclipse, jars, and noClassDefFoundError
NoClassDefFoundError
NoClassDefFoundError
NoClassDefFoundError