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.URLClassLoader1ドル.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?
6 Answers 6
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.
Comments
The 'dot' is a delimiter. When you wrote Welcome.class, it was looking for a class named 'class' that is in the 'Welcome' package.
Comments
The parameter you pass to java.exe is the class name (with optional package), not the file name.
Regards.
Comments
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
Comments
First you need to give javac file_name.java after that Compiler will compile the java program and it generates the dot(.)class file.so you cannot directly give the class file without compiling the program.