1
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?

asked Jul 2, 2009 at 15:28

6 Answers 6

2

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.

answered Jul 2, 2009 at 15:32
Sign up to request clarification or add additional context in comments.

Comments

2

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

answered Jul 2, 2009 at 15:33

Comments

2

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.

answered Jul 2, 2009 at 15:37

Comments

1

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

Regards.

answered Jul 2, 2009 at 15:33

Comments

1

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

answered Jul 2, 2009 at 15:35

Comments

-2

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.

answered Oct 3, 2013 at 13:06

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.