• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

NoClassDefFoundError pergatory

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks much, Ernest and Jesper. That's just what I needed.
 
Anything worth doing well is worth doing poorly first. Just look at this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic