Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-19-2012, 08:25 PM   PM User | #1
Jemdt
New Coder

 
Join Date: May 2012
Posts: 29
Thanks: 2
Thanked 0 Times in 0 Posts
Jemdt is an unknown quantity at this point
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
Jemdt is offline   Reply With Quote
Old 06-19-2012, 10:09 PM   PM User | #2
jordandap
New Coder

 
Join Date: Jul 2011
Posts: 44
Thanks: 5
Thanked 0 Times in 0 Posts
jordandap is an unknown quantity at this point
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 10:12 PM..
jordandap is offline   Reply With Quote
Old 06-19-2012, 10:45 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,659
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 06-20-2012, 05:42 PM   PM User | #4
Jemdt
New Coder

 
Join Date: May 2012
Posts: 29
Thanks: 2
Thanked 0 Times in 0 Posts
Jemdt is an unknown quantity at this point
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.
Jemdt is offline   Reply With Quote
Old 06-20-2012, 06:05 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,659
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Code:
> cd /d C:\MyLocation
> java HelloWorld
Try that.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
Jemdt (06-25-2012)
Old 06-22-2012, 06:06 AM   PM User | #6
michaelh73
New Coder

 
Join Date: Jun 2012
Posts: 15
Thanks: 0
Thanked 3 Times in 3 Posts
michaelh73 is an unknown quantity at this point
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
michaelh73 is offline   Reply With Quote
Old 06-22-2012, 07:31 PM   PM User | #7
Jemdt
New Coder

 
Join Date: May 2012
Posts: 29
Thanks: 2
Thanked 0 Times in 0 Posts
Jemdt is an unknown quantity at this point
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
Jemdt is offline   Reply With Quote
Old 06-22-2012, 07:38 PM   PM User | #8
Jemdt
New Coder

 
Join Date: May 2012
Posts: 29
Thanks: 2
Thanked 0 Times in 0 Posts
Jemdt is an unknown quantity at this point
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
Jemdt is offline   Reply With Quote
Old 06-25-2012, 10:00 PM   PM User | #9
Jemdt
New Coder

 
Join Date: May 2012
Posts: 29
Thanks: 2
Thanked 0 Times in 0 Posts
Jemdt is an unknown quantity at this point
Solved, thanks to Fou-Lou!
Jemdt is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:27 PM.


Advertisement
Log in to turn off these ads.