Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why do I have to specify java -cp . ?  RSS feed

 
Thomas Autera
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do I have to specify java -cp . HelloWorld, when the class is in the current directory?
Here's what's goin on:
1) I have HelloWorld.java code in my /junk dir.
2) I compile the program, and the class file is output to /junk dir.
3) I execute command "java HelloWorld" (in /junk ) and receive the following:
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
4) When I add "-cp ." it works fine (java -cp . HelloWorld).
Here's my (WIN XP) DOS session:
C:\Documents and Settings\Thomas Autera\junk>javac HelloWorld.java
C:\Documents and Settings\Thomas Autera\junk>dir Hello*
Volume in drive C is HP_PAVILION
Volume Serial Number is 2861-1CE2
Directory of C:\Documents and Settings\Thomas Autera\junk
01/25/2004 12:30 PM 112 HelloWorld.java
01/25/2004 12:30 PM 112 HelloWorld.java.bak
01/25/2004 12:31 PM 426 HelloWorld.class
3 File(s) 650 bytes
0 Dir(s) 6,425,575,424 bytes free
C:\Documents and Settings\Thomas Autera\junk>java HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
C:\Documents and Settings\Thomas Autera\junk>java -cp . HelloWorld
Hello World!
C:\Documents and Settings\Thomas Autera\junk>
Here's the program source:
public class HelloWorld{
public static void main(String[] args){
System.out.println("Hello World!");
}
}
Thanks, hope you can help.
Tom
 
Marilyn de Queiroz
Sheriff
Posts: 9080
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may have to include the '.' in your classpath environment variable. For a while it was not necessary, but it seems recently that this problem has been popping up again.
 
Thomas Autera
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Marilyn, that works.
Do you think this a Java bug, or possibly a system configuration issue?
 
Ken Krebs
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas,
It's not a bug, that's just the way it works.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!