Results 1 to 9 of 9
  1. #1
    MadDogMullan is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Smile Exception in thread "main" java.lang.NoClassDefFoundError: welcome

    Hi all,

    Just started the install went well and the compiler (javac) works and creates .class files. However, everything I run "java" I always get the same error and to the best of my know I have done everything right.

    The full error message is:

    F:\My Documents\Java Files>javac welcome.java the file compiles ok

    F:\My Documents\Java Files>java welcome
    Exception in thread "main" java.lang.NoClassDefFoundError: welcome
    Caused by: java.lang.ClassNotFoundException: welcome
    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)

    Can someone help please.

    Many thanks in advance.

    Peter

  2. #2
    mwildam's Avatar
    mwildam is offline Member
    Join Date
    Aug 2007
    Posts
    72
    Rep Power
    0

    Default

    What about posting your source?
    Greetings, Martin Wildam.

  3. #3
    MadDogMullan is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default Here is the source

    public class welcome
    {
    public static void main(String[ ] args)
    {
    String[ ] greeting = new String[3];
    greeting[0] = "Welcome tp Core Java";
    greeting[1] = "by Peter Mullan";
    greeting[2] = " & asdjf";

    for (String g : greeting)
    System.out.println(g);
    }
    }

    But it happens to any simple program I write.

    Many thanks.

    Peter

  4. #4
    mwildam's Avatar
    mwildam is offline Member
    Join Date
    Aug 2007
    Posts
    72
    Rep Power
    0

    Default

    First: A class name should start with an uppercase letter. Compiler should give you a warning on this.

    Second: When launching your program with java you should specify the classpath commandline option - that is telling java where to find the class (I imagine your current dir is the the bin folder of the jdk ;) )

    Third: When working with more classes they should be organized in packages and folder structure should be according to the package name - see for example here: The Java Package Tutorial
    Greetings, Martin Wildam.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,560
    Rep Power
    25

    Default

    Uppercase is only a convention. Lower case will work.
    You don't need to set the classpath on your java command, but it is recommended.

    Try: java -cp . welcome

    Do you have a CLASSPATH environment variable set on your system? Does it have a . in it? That tells java to look in the current directory. Without the . it will NOT look in the current directory.

  6. #6
    MadDogMullan is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default It's working

    Thank you for all your help.

    The way I got it to work was to download and install Netbeans, which is a very nice java editor, which I was looking for anyway and it has the added advance in allowing you to execute your class files always, so all is good.

    Once again many thanks for your help:)

    Peter Mullan

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,560
    Rep Power
    25

    Default

    IDEs can be a help and a hinderance. If you rely on the IDE to solve problems as you've had, then you won't learn how java works in the OS environment. When you move out of the IDE you'll be lost. See you later.

  8. #8
    mwildam's Avatar
    mwildam is offline Member
    Join Date
    Aug 2007
    Posts
    72
    Rep Power
    0

    Default

    Actually I tried it also with NetBeans where I only had to choose the main class. But the IDE saves it and creates the appropriate config file for you.

    I agree that you should know such basics anyway so that you can handle it also without the IDE.

    But then it is good to have a tool that solves the library packaging and creates the jar file and appropriate distribution folder so that you can easily distribute everything necessary.
    Greetings, Martin Wildam.

  9. #9
    MadDogMullan is offline Member
    Join Date
    Sep 2008
    Posts
    4
    Rep Power
    0

    Default

    Thanks Norm and Martin.

    I agree about knowing what happens in the OS environment but as I am just starting out it is more important for me to know what I am doing works. When I get into it proper I will figure out why it does work in dos prompt.

    Regards,

    Peter

Similar Threads

  1. Replies: 6
    Last Post: 08-18-2008, 06:44 AM
  2. Replies: 1
    Last Post: 08-07-2007, 07:10 AM
  3. Replies: 1
    Last Post: 08-06-2007, 06:07 AM
  4. Replies: 2
    Last Post: 08-01-2007, 05:26 AM
  5. Replies: 1
    Last Post: 07-25-2007, 10:55 PM

Posting Permissions

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