Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NoClassDefFoundError

 
george zhao
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote a few codes.

public class Hello{

public static void main( String args[] ){

System.out.println( "My test" );

}

}

then " javac Hello.java "

then " java Hello",
System throws out "Exception in thread "main" java.lang.NoClassDefFoundError: Hello"

In WindowsXp's cmd,I use "echo %classpath%" ,it returns:
"D:\Program Files\Java\jdk1.5.0_11\bin;D:\Program Files\Java\jdk1.5.0_11\lib\tools.jar;D:\Program Files\Java\jdk1.5.0_11\jre\lib\rt.jar;D:\Program Files\Apache Software Foundation\Tomcat 5.5common\lib"

I suspected I set wrong classpath. Pls help me.
[ July 18, 2007: Message edited by: george zhao ]
 
Mike Guze
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
use "java -cp . Hello"

with -cp option you are telling to JVM where to find your classes
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by george zhao:
...In WindowsXp's cmd,I use "echo %classpath%" ,it returns:
"D:\Program Files\Java\jdk1.5.0_11\bin;D:\Program Files\Java\jdk1.5.0_11\lib\tools.jar;D:\Program Files\Java\jdk1.5.0_11\jre\lib\rt.jar;D:\Program Files\Apache Software Foundation\Tomcat 5.5common\lib"...

If you're going to have a system classpath, it should probably include a dot (.) for the current directory.
 
george zhao
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That does really help me lot.
Thanks a million , both of you.
"." means current folder, does it?

And
if JDK's version is below 1.5, pls "java -classpath . Hello"
[ July 18, 2007: Message edited by: george zhao ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by george zhao:
... "." means current folder, does it? ...

Yes. Without that, the JVM would look for the class in each of the paths listed in the classpath, and when it can't find it, it would throw a NoClassDefFoundError. See How classes are found in the Java documentation.

Both -cp and -classpath were java options in 1.4. The documentation omits -cp as a javac option (specifying only -classpath), but as I recall, -cp did work.
[ July 19, 2007: Message edited by: marc weber ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic