views:

291

answers:

5
C:\Program Files\Java\jdk1.6.0_05\CoreJava\v1\v1ch2\Welcome>javac Welcome.java

C:\Program Files\Java\jdk1.6.0_05\CoreJava\v1\v1ch2\Welcome>java Welcome.class
Exception in thread "main" java.lang.NoClassDefFoundError: Welcome/class
Caused by: java.lang.ClassNotFoundException: Welcome.class
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)

C:\Program Files\Java\jdk1.6.0_05\CoreJava\v1\v1ch2\Welcome>java Welcome
Welcome to Core Java
by Cay Horstmann
and Gary Cornell

So my problem is,how does "java" find and executes a class?why can not directly specify the class file?

+2  A: 

If you add the .class java thinks you are looking for class named "class" in the package "Welcome". since there is not one you get an error.

Nir Levy
+1  A: 

Hi,

The parameter you pass to java.exe is the class name (with optional package), not the file name.

Regards.

ATorras
+2  A: 

The 'dot' is a delimiter. When you wrote Welcome.class, it was looking for a class named 'class' that is in the 'Welcome' package.

Rick
+1  A: 

Its interpreting the dot in your filename as a package designation. As you advance in your knowledge of java, you will learn about packages and find that normally your class files are within a package, so for example, if the Welcome class was in the package "com.ericasberry", I would run it by typing java com.ericasberry.Welcome

Eric Asberry
+1  A: 

The java program expects a class name as parameter, not a file name. As stated in the java manual: ( java )

java [ options ] class [ argument ... ]

Once this is clear read about the classpath.

PeterMmm