Results 1 to 9 of 9
Thread: Command Line Error
- 07-29-2010, 08:15 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 6
- Rep Power
- 0
Command Line Error
I was going through the Java Core Vol I and there is a small class they have you compile from the command line. When using the javac Welcom.java everything goes fine. Once I try the java Welcome.class I receive an error. This is the class followed by the error message. The whole thing compiles perfectly fine in eclipse. What would cause it to error out on the command line? :eek:
public class Welcome
{
public static void main(String[] args)
{
String[] greeting = new String[3];
greeting[0] = "Welcome to Core Java";
greeting[1] = "by Cay Horstmann";
greeting[2] = "and Gary Cornell";
for (int i = 0; i < greeting.length; i++)
System.out.println(greeting[i]);
}
}
C:\CoreJaveBook\v1ch02\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(URLClassLoader.java: 202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 07)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 48)
Could not find the main class: Welcome.class. Program will exit.
Any help would be great! Thank you!Last edited by Differintegral; 07-29-2010 at 08:44 AM.
- 07-29-2010, 08:25 AM #2
Welcome.class is not on the classpath, the jre doesn't know where to look for it.
That's why you should not use eclipse until you've mastered path, classpath and packaging mechanisms.
Read here: http://download.oracle.com/javase/tu...ems/index.htmlMath problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 07-29-2010, 08:28 AM #3
Member
- Join Date
- Jul 2010
- Posts
- 6
- Rep Power
- 0
Yeah it was just user error after a long night. Thanks!
- 07-29-2010, 08:35 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
javac compiles files but java runs classes; the name of the class is 'Welcome' and the file it is stored in is 'Welcome.class'; See how java had changed it to Welcome/class, assuming that Welcome is a package name; you should give java a class name (without the .class extension]
But PhHein is right: first you should study packages and classes and the path and classpath variables.Java Code:java Welcome
kind regards,
Jos
- 07-29-2010, 08:38 AM #5
Member
- Join Date
- Jul 2010
- Posts
- 6
- Rep Power
- 0
Thanks a lot for the information and help. I wasnt really paying attention. I already had the path and classpath set up. I just needed to pay attention to what I was typing. Thanks again!!!
- 07-29-2010, 08:44 AM #6
Member
- Join Date
- Jul 2010
- Posts
- 6
- Rep Power
- 0
Well I thought the classpath was set but it was not. The path was set of course or I would have never gotten the error message. The thing is the java Welcome works without the classpath set. How come?
- 07-29-2010, 08:45 AM #7
Wow, lack of coffee. I didn't notice the .class
Thanks Jos!Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 07-29-2010, 08:51 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,422
- Blog Entries
- 7
- Rep Power
- 17
If I'm not mistaken on a MS Windows machine the current directory is assumed to be in the classpath; I never assume anything:
The classpath values can also explicitly be set on the command line following the -cp flag.Java Code:java -cp . Welcome
kind regards,
Jos
- 07-29-2010, 09:49 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
Command Line Error
By king2be98 in forum New To JavaReplies: 5Last Post: 10-25-2010, 08:54 PM -
Formatting java command line output - Multi line string
By dricco in forum New To JavaReplies: 2Last Post: 07-02-2010, 02:20 PM -
Compiling/Running Project in Command Line: "Could not find main class" Error
By Yasemin Gokce in forum New To JavaReplies: 1Last Post: 06-30-2009, 02:32 PM -
Error Occurred Executing Command Line Solution
By RahulKhire in forum EclipseReplies: 1Last Post: 09-10-2008, 01:38 PM -
Unable to execute command line command in java
By LordSM in forum New To JavaReplies: 1Last Post: 08-08-2007, 12:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks