February 5th, 2013, 04:22 PM
The difference in the command line when executing a Java programming
I'm a noob at this Java programming business and first time user of the Shed. Literally just started last night... So here's the story... Last night I was going through the typical HelloWorld Application, simple. Compiled the code with javac in the command land and executed using java HelloWorldApp.class
That was fine, it worked first time.
This evening, i go back to it and use the same line to have a look at it again.. Doesn't work.. I got a "Could not find or load main class HelloWorldApp.class"
From googling the life out of this problem the solution was to type in
java -cp . HelloWorldApp
So, for the love of all that is google - can someone tell me what the difference is? and why was it working the first way last night and not tonight?
Really appreciate your help with this.
February 5th, 2013, 04:44 PM
The file extension (.class) should not be used when calling java.
You should also be able to run this command: java HelloWorld
February 6th, 2013, 08:48 AM
Thanks for that... that worked also!
February 6th, 2013, 04:09 PM
The case where the -cp . option is used would be for when the classpath environment variable is set and it does not include the .
If you entered:
with the TheClass.class file not being in a folder on the classpath set in the environment variable, the java program would not find the definition for TheClass.
java -cp . TheClass
would override the environment variable, specifying the current directory as being the classpath where the java program should for the definition of TheClass.