Reputation: 139862
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?
Upvotes: 1
Views: 1761
Reputation: 928
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.
Upvotes: -2
Reputation: 24630
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.
Upvotes: 2
Reputation: 612
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
Upvotes: 1
Reputation: 3840
The 'dot' is a delimiter. When you wrote Welcome.class, it was looking for a class named 'class' that is in the 'Welcome' package.
Upvotes: 2
Reputation: 4303
The parameter you pass to java.exe is the class name (with optional package), not the file name.
Regards.
Upvotes: 1
Reputation: 4740
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.
Upvotes: 2