Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default 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.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    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.html
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default

    Yeah it was just user error after a long night. Thanks!

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,563
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Differintegral View Post
    C:\CoreJaveBook\v1ch02\Welcome>java Welcome.class
    Exception in thread "main" java.lang.NoClassDefFoundError: Welcome/class
    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]

    Java Code:
    java Welcome
    But PhHein is right: first you should study packages and classes and the path and classpath variables.

    kind regards,

    Jos

  5. #5
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default

    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!!!

  6. #6
    Join Date
    Jul 2010
    Posts
    6
    Rep Power
    0

    Default

    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?

  7. #7
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    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

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,563
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Differintegral View Post
    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?
    If I'm not mistaken on a MS Windows machine the current directory is assumed to be in the classpath; I never assume anything:

    Java Code:
    java -cp . Welcome
    The classpath values can also explicitly be set on the command line following the -cp flag.

    kind regards,

    Jos

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,040
    Rep Power
    20

    Default

    Even the java command assumes the current directory is on the classpath unless '-cp' has been given, or '-jar', in which case it takes only the values in the supplied classpath (either after -cp, or in the manifest for a jar).

Similar Threads

  1. Command Line Error
    By king2be98 in forum New To Java
    Replies: 5
    Last Post: 10-25-2010, 08:54 PM
  2. Replies: 2
    Last Post: 07-02-2010, 02:20 PM
  3. Replies: 1
    Last Post: 06-30-2009, 02:32 PM
  4. Error Occurred Executing Command Line Solution
    By RahulKhire in forum Eclipse
    Replies: 1
    Last Post: 09-10-2008, 01:38 PM
  5. Unable to execute command line command in java
    By LordSM in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 12:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •