#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    Ireland
    Posts
    3
    Rep Power
    0

    The difference in the command line when executing a Java programming


    Hi Guys,

    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
    rather than
    java HelloWorldApp.class

    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.

    Thanks,
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,707
    Rep Power
    1959
    The file extension (.class) should not be used when calling java.
    You should also be able to run this command: java HelloWorld
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    Ireland
    Posts
    3
    Rep Power
    0
    Thanks for that... that worked also!
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,719
    Rep Power
    348
    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:
    java TheClass
    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.
    Entering:
    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.

IMN logo majestic logo threadwatch logo seochat tools logo