posted 19 years ago
Hi,
When I appended a "." to the classpath, I get the following results:
D:\sandbox\project1\src->echo %CLASSPATH%
d:\jdk-1_5_0_09\lib;D:\Apache\Tomcat_5_5\common\lib;.
D:\sandbox\project1\src->type HelloWorld.java
public class HelloWorld
{
public static void main (String[] args)
{
System.out.println("Hello World");
}
}
D:\sandbox\project1\src->java HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
D:\sandbox\project1\src->java HelloWorld.java
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld/java
D:\sandbox\project1\src->javac HelloWorld.java
D:\sandbox\project1\src->java HelloWorld
Hello World
As you can see, if I compile it first, then run it from the *.class, it works. I then tried to force the classpath as you suggested:
D:\sandbox\project1\src->del *.class
D:\sandbox\project1\src->java -cp . HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
D:\sandbox\project1\src->java -cp . HelloWorld.java
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld/java
D:\sandbox\project1\src->java -classpath . HelloWorld.java
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld/java
D:\sandbox\project1\src->java -classpath . HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
D:\sandbox\project1\src->
I am lead to believe that if a classpath is not specifically defined, the current directory is tried first. I have tried specifying a classpath, forcing a classpath locally and clearing the classpath and forcing the classpath on the command line, with the same result: it will work only if I compile it firat, then execute it from the class. I cannot get it to compile and execute directly such as "java HelloWorld"
I guess it is not a big deal, I can build a batch file that will do that for me, I would just like the command line invocation to work like the book advertizes.