Page 1 of 2 12 LastLast
Results 1 to 20 of 30
Like Tree2Likes

Thread: Error: Could not find or load main class HelloWorldApp (HELP)

  1. #1
    couch_mango is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Question Error: Could not find or load main class HelloWorldApp (HELP)

    I'm a total noob trying to learn Java so I can make games and programs.
    Ill show you the steps that led to this.

    -Opened Notepad and typed the "HelloWorld" source code.

    -Saved file as HelloWorld.java to desktop.

    -Went to Computer properties and set path to C:\"Program Files"\Java\jdk1.7.0\bin

    -Opened Command Prompt

    - Typed "cd Desktop".

    -Typed "Javac HelloWorldApp"

    -File compiles into a "HelloWorldApp.class" file.

    -Typed Java HelloWorldApp

    -Error: Could not find or load main class HelloWorldApp

    Help :L

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    The class (HelloWorld), the filename (HelloWorld.java), and the class you specify to run (HelloWorld) must all agree. In your case you say you "Typed Java HelloWorldApp" but the command should have been

    Java Code:
    C:\<whereever-your-desktop-is>\>java HelloWorld
    If you still get the "Could not find or load main class" error post a couple of things: (1) The source code (.java file) and (2) the actual output from the command line when you attempt to compile and run it.

    [Edit] Or you may have meant the class to be HelloWorldApp in which case the moral is the same: the source file should be HelloWorldApp.java

    For step by step details (in case you aren't actually following this page!): Oracle's Tutorial
    Last edited by pbrockway2; 08-07-2011 at 03:47 AM.

  3. #3
    couch_mango is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default

    So How do I exit a.. um a section. As in (C:\) (\Desktop\)

  4. #4
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    -Saved file as HelloWorld.java to desktop.
    -Typed "Javac HelloWorldApp"
    -File compiles into a "HelloWorldApp.class" file.

    How did that come to be?


    The basic syntax (can be modified) of the main method is as follows, but remove all *, they just indicate important parts that you might have missed:

    Java Code:
    *public* class HelloWorldApp
    {
        *public* *static* void main(*String[]* args)
        {
            System.out.println("It works! ☺");
        }
    }
    Last edited by Hibernate; 08-07-2011 at 03:59 AM. Reason: Forgot code tag
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  5. #5
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Quote Originally Posted by couch_mango View Post
    So How do I exit a.. um a section. As in (C:\) (\Desktop\)
    I do not understand. Clarification?
    Last edited by Hibernate; 08-07-2011 at 04:05 AM. Reason: I love those incorrectly rotated quotes ☺
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  6. #6
    couch_mango is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by Hibernate View Post
    -Saved file as HelloWorld.java to desktop.
    -Typed "Javac HelloWorldApp"
    -File compiles into a "HelloWorldApp.class" file.

    How did that come to be?


    The basic syntax (can be modified) of the main method is as follows, but remove all *, they just indicate important parts that you might have missed:

    Java Code:
    *public* class HelloWorldApp
    {
        *public* *static* void main(*String[]* args)
        {
            System.out.println("It works! ☺");
        }
    }
    Now it says 'javac' is not recognized as an internal or external command.

  7. #7
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    But it was recognised before?

    Did you try running?:
    "C:\Program Files\Java\jdk1.7.0\bin\javac" HelloWorldApp.java
    or (but I don't think it is necessary)
    "C:\Program Files\Java\jdk1.7.0\bin\javac.exe" HelloWorldApp.java
    Last edited by Hibernate; 08-07-2011 at 04:10 AM. Reason: nicer syntax
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  8. #8
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Quote Originally Posted by couch_mango View Post
    -Went to Computer properties and set path to C:\"Program Files"\Java\jdk1.7.0\bin
    I hope you appended (or otherwise inserted)
    Java Code:
    ;C:\Program Files\Java\jdk1.7.0\bin
    And not remove all that was already there.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  9. #9
    couch_mango is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default

    Ok i compiled it now. I had to type:

    C:\"Program Files"\Java\jdk1.7.0\bin\javac HelloWorldApp.java

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Sorry for the repetition, but could you post the source code you are using and the actual command line output you get when you attempt to compile and run it?

    [Edit] too slow ;( Glad you've got it compiled now.

  11. #11
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    I guess there is a post here somewhere on how to do so that you just need to type javac, and not its absolute path.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  12. #12
    couch_mango is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default

    public class HelloWorldApp
    {
    public static void main(String[] args)
    {
    System.out.println("It works! ☺");
    }
    }



    Compiles fine.

    When i try to run it says: Error: Could not find or load main class HelloWorldApp

  13. #13
    couch_mango is offline Member
    Join Date
    Aug 2011
    Posts
    6
    Rep Power
    0

    Default

    I still cant "Run" the class file.

  14. #14
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    java HelloWorldApp
    or
    "C:\Program Files\Java\jdk1.7.0\bin\javac" HelloWorldApp

    It should not(!!) end with .class


    If that does not work, what does it say?
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  15. #15
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Quote Originally Posted by couch_mango View Post
    I still cant "Run" the class file.
    Run is allowed to say, so you do not need quotes, but the proper word is execute.
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  16. #16
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    Quote Originally Posted by couch_mango View Post
    When i try to run it says: Error: Could not find or load main class HelloWorldApp
    Missed that post.


    Could you post the line you feed the command prompt to get that error?
    Ex animo! Hibernate
    Java, Arch Linux, C, GPL v3, Bash, Eclipse, Linux VT, GNOME 2 and many buttons on windows.

  17. #17
    Adithi is offline Member
    Join Date
    Aug 2011
    Posts
    14
    Rep Power
    0

    Default PlZ Reply.. i have the same problem..

    Hello.java:

    class Hello{
    public static void main(String args[]){
    System.out.println("Hello...");
    }
    }

    when compiled, its fine and gives me a .class file.
    but when tried to execute the program:
    Error: Could not find or load main class Hello.

    I have my env variables set this way:

    CLASSPATH
    C:\Program Files\Java\jdk1.7.0\lib

    JAVA_HOME
    C:\Program Files\Java\jdk1.7.0

    Path
    C:\Program Files\Java\jdk1.7.0\bin;

    I have jdk1.7.0 installed on my machine, but the jre is jre6 (C:\Program Files\Java\jre6). Is that a problem that versions of jdk and jre are different.

    Reply would be a great help. Thanks

  18. #18
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You may want to make a new post, rather than adding it to this one. Even if it is a similar question.

  19. #19
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Remove the CLASSPATH variable unless you set it for some reason. Run the program with

    Java Code:
    java -cp . Hello
    It is the dash-cp-space-dot which sets the classpath that the java exectable will use (to the current dirctory). Using an OS variable for this purpose is not very flexible and setting the classpath to the lib directory makes no sense.

    If this does not run your program, start your own thread although for all we can tell the OP may be making the same mistake.

  20. #20
    Adithi is offline Member
    Join Date
    Aug 2011
    Posts
    14
    Rep Power
    0

    Default

    Thanks.It works with this command java -cp . But do I need to change any settings or so.. in order to avoid typing -cp . every time i execute a program? Do you know why I had this problem. Because generally its just the 'java' command that is used to execute a simple java program.

    Thanks again for the reply..

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 09-22-2011, 09:48 PM
  2. runtime error 'could not find main class' ?
    By ruud00000 in forum Java Applets
    Replies: 5
    Last Post: 12-31-2010, 05:58 PM
  3. Replies: 3
    Last Post: 11-11-2010, 08:12 AM
  4. [SOLVED] Could not find the main class: error...
    By bobleny in forum New To Java
    Replies: 21
    Last Post: 03-02-2010, 10:53 AM
  5. Replies: 1
    Last Post: 07-03-2009, 08:41 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
  •