Results 1 to 10 of 10
  1. #1
    TenF is offline Member
    Join Date
    Aug 2009
    Posts
    8
    Rep Power
    0

    Default Class Not Found command promt

    Hi,

    I'm not really a beginner in Java, but since I've never worked from the command prompt before, I guess I am a beginner at that ;)

    I've been trying to start a Java application from the command prompt, but I keep getting the error message that dhe class could not be found.
    I've searched the internet a lot and tested things, but I can't figure out what the problem is. To make things simple I made a Hello World application which gives the same error.

    I made the following program, naming it HelloWorld.java and saving it in the directory C:\mywork
    Java Code:
    public class HelloWorld {
        public static void main(String[] args) {
            System.out.println("Hello World!");
        }
    }
    I then started the command prompt and compiled the program using
    javac HelloWorld.java
    When I tried to run it using
    java HelloWorld
    I got the following error message:
    Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
    Caused by: <list of things>
    Could not find the main class: HelloWorld. Program will exit.

    What am I missing? Please help. I need to test my application, it's an assignment for school and I have to hand it in in a week...

  2. #2
    TenF is offline Member
    Join Date
    Aug 2009
    Posts
    8
    Rep Power
    0

    Default

    Ok, I got some help elsewhere, but I don't understand what was wrong. So if someone could explain to me what the problem was, I would be grateful.
    The solution was to start the application using the following
    java -classpath . HelloWorld

    I was told I had to change the CLASSPATH variable to solve the problem permanently, by putting a dot in front of the current CLASSPATH. But the dot was already in place.
    Anyone have a clue as to why things are acting this way?

    Edit: Oh, and I'm sorry for the double posting, but it is kind of a reply to my earlier message, not an addition or something ;)

  3. #3
    weddy69 is offline Member
    Join Date
    Aug 2009
    Posts
    5
    Rep Power
    0

    Default

    depends on the operating system you have but I believe you just run your jar
    java -jar myjar.jar

    quick search on this should help a lot.

  4. #4
    TenF is offline Member
    Join Date
    Aug 2009
    Posts
    8
    Rep Power
    0

    Default

    Thank you for your reply.

    However, I don't have any jar-file. I was just working with .java and .class files.

    And I did more than a quick search, I did a thorough search.
    But as I said, the solution was something that shouldn't have worked. I had checked my classpath, but it seemed fine. So adding "-classpath . " shouldn't have changed anything.

  5. #5
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    Print out the entire command line session of you trying to compile/run it so we can see everything.

    Here is mine

    Java Code:
    joe@joe:~/Projects$ ls
    cdk  Main.java
    joe@joe:~/Projects$ javac Main.java
    joe@joe:~/Projects$ ls
    cdk  Main.class  Main.java
    joe@joe:~/Projects$ java Main
    Hello World
    Mr. Beans

  6. #6
    TenF is offline Member
    Join Date
    Aug 2009
    Posts
    8
    Rep Power
    0

    Default

    Java Code:
    C:\mywork>javac HelloWorld.java
    
    C:\mywork>java HelloWorld
    Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld
    Caused by: java.lang.ClassNotFoundException: HelloWorld
            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)
    Could not find the main class: HelloWorld.  Program will exit.
    
    C:\mywork>java -classpath . HelloWorld
    Hello World!
    
    C:\mywork>
    Java Code:
    C:\mywork>path
    PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32\WBEM;C:\Program Files\Au
    todesk\Maya 8.5 Personal Learning Edition\bin;C:\Program Files\MiKTeX 2.7\miktex
    \bin;C:\Program Files\Common Files\Teleca Shared;E:\Programma's\TortoiseSVN\bin;
    C:\Program Files\QuickTime\QTSystem\;C:\Program Files\Java\jdk1.5.0_04\bin;E:\Pr
    ogramma's\OpenVPN\bin
    I noticed that the path doesn't have a dot in front, but if I go to environment variables (properties of My Computer, Advanced tab) the classpath variable does have a dot...

  7. #7
    Spiller is offline Member
    Join Date
    Aug 2009
    Posts
    4
    Rep Power
    0

    Default

    Tip: Use the command set to see/edit current environment variables from the command line.

    Java won't look for classes in the other, ordinary path, though the dot directory is implicit in that, by the way.

  8. #8
    TenF is offline Member
    Join Date
    Aug 2009
    Posts
    8
    Rep Power
    0

    Default

    So I typed 'set' followed by Enter.
    The path is as posted before. The classpath is '. (period)'
    no idea what that means.

    In what way should I edit which variable?

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    have you tried compiling like so:
    Java Code:
    javac -cp . HelloWorld.java
    (don't forget the period in there)

    Edit: also, are you placing your java file in a package and giving it a package name (my guess is not)?
    Last edited by Fubarable; 08-17-2009 at 10:59 PM.

  10. #10
    TenF is offline Member
    Join Date
    Aug 2009
    Posts
    8
    Rep Power
    0

    Default

    Ok, I tried compiling with the -cp . added
    When trying to run the application, I got the same errors, so I still had to add -cp . when running it.

    And no, no package.
    As I said, I am currently testing with simply a HelloWorld.java, which is really nothing more than a good old fashioned Hello World program. The code of this "program" is in my first post.

    And thanks for the replies, I really appreciate you all trying to help :)

Similar Threads

  1. Error: no class definition found
    By toby in forum New To Java
    Replies: 6
    Last Post: 08-28-2011, 10:32 PM
  2. Class not found Exception
    By surendra in forum Java Servlet
    Replies: 8
    Last Post: 06-09-2011, 12:52 PM
  3. No Class Def Found Error:
    By fithous in forum Java Applets
    Replies: 0
    Last Post: 04-02-2009, 05:30 PM
  4. JRE rtapplet class not found error
    By avinash.natekar in forum Java Applets
    Replies: 11
    Last Post: 04-02-2009, 08:02 AM
  5. No Class Def Found Error:
    By wrap23 in forum New To Java
    Replies: 9
    Last Post: 10-02-2008, 04:07 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
  •