Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    May 2012
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Class will not execute

    Hi, I'm fairly new to problem, and I've instantly come across the problem which appears that every newbie seems to come across.

    I'm working off a book, on to which a very simple system.out.println code was given out (below):

    Code:
    public class HelloWorld {
    
    	public static void main(String[] args) {
    		System.out.println("Hello World!");
    	}
    
    }
    The .java file compiles without any errors at all and a HelloWorld.class file is created. However, whenever I type in either:

    Code:
    java C:\MYLOCATION\HelloWorld
    OR

    Code:
    java -cp . C:\MYLOCATION\Helloworld
    I get the same error:

    "Error: Could not find or load the main class HelloWorld"

    The class runs fine on Eclipse, but I'd like to know how to do it correctly without the use of an IDE.

    Thanks,

    James

  • #2
    New Coder
    Join Date
    Jul 2011
    Posts
    44
    Thanks
    5
    Thanked 0 Times in 0 Posts
    just a small error qouted the error and put the correct statment on top you just had the [] in the wrong place

    Code:
    public class HelloWorld {
            public static void main (String args[]) {
    	//public static void main(String[] args) {
    		System.out.println("Hello World!");
    	}
    
    }
    Last edited by jordandap; 06-19-2012 at 11:12 PM.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    No, that's not correct. Main actually accepts several different signatures. Offhand these will all work (assuming at least 5 since it uses vargs):
    PHP Code:
    public static void main(String[] argv);
    public static 
    void main(String argv[]);
    public static 
    void main(String... argv); 
    You can actually just give it a string as well.

    For the OP, change your directory to the path to execute. When you issue path/class it expects that as the package of the class itself from the -cp directives set. This has no package, so you want to simply use java HelloWorld from the C:\MyLocation directory.

    Edit:
    BTW, the most common and best suited signature is public static void main(String[] argv) as it clearly identifies the String as the array.

  • #4
    New Coder
    Join Date
    May 2012
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post

    You can actually just give it a string as well.

    For the OP, change your directory to the path to execute. When you issue path/class it expects that as the package of the class itself from the -cp directives set. This has no package, so you want to simply use java HelloWorld from the C:\MyLocation directory.
    Sorry, I'm not quite sure what you mean. Could you possibly give an example? Thanks.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Code:
    > cd /d C:\MyLocation
    > java HelloWorld
    Try that.

  • Users who have thanked Fou-Lu for this post:

    Jemdt (06-25-2012)

  • #6
    New Coder
    Join Date
    Jun 2012
    Posts
    15
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by Jemdt View Post
    Code:
    java -cp . C:\MYLOCATION\Helloworld
    I'm not sure if you did this while you were trying to run it, but java is case sensitive so your class name is HelloWorld, and not Helloworld like you've shown above.

    But changing directories like Fou-Lu said above will make your life easier, and also help keep you from making tiny errors

  • #7
    New Coder
    Join Date
    May 2012
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Code:
    > cd /d C:\MyLocation
    > java HelloWorld
    Try that.
    Didn't work, and neither did Michellah's...

    Nevermind, thanks for helping, but I'll just stick to Eclipse.

    Thanks again

  • #8
    New Coder
    Join Date
    May 2012
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    Code:
    > cd /d C:\MyLocation
    > java HelloWorld
    Try that.
    Didn't work, and neither did Michellah's...

    Nevermind, thanks for helping, but I'll just stick to Eclipse.

    Thanks again

  • #9
    New Coder
    Join Date
    May 2012
    Posts
    29
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Solved, thanks to Fou-Lou!


  •  

    Posting Permissions

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