Results 1 to 5 of 5
  1. #1
    dunboody is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

    Default Why does my java command only work when I put "-cp ." before it?

    I'm new to programming, so I tried to make the "Hello World" thing. I wrote the code for Hello World and compiled it just fine in the command prompt. Then when I tried to run it, it didn't work. I put in "java Hello World", and it responded with "could not find or load main class Hello World". So, I tried this instead: "java -cp . Hello World", and it worked just fine. So why does adding the cp part make it work?

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

    Default Re: Why does my java command only work when I put "-cp ." before it?

    The -cp flag sets the classpath for the jvm to search in. You can set it in the system variables, but some will say using -cp is better.

  3. #3
    dunboody is offline Member
    Join Date
    Sep 2011
    Posts
    10
    Rep Power
    0

    Default Re: Why does my java command only work when I put "-cp ." before it?

    Gracias, sir.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Why does my java command only work when I put "-cp ." before it?

    Have you set the system environment variable: Classpath?
    It will override the default current directory. Which means you have to use -cp . if that value is not in the variable.
    If you have set the environment variable without the ., add ;. to the end of it to have the current directory on the classpath.

    See your settings by opening a command prompt and entering: set

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Why does my java command only work when I put "-cp ." before it?

    If you have set the environment variable without the ., add ;. to the end of it
    I would recommend adding .; (or .: on *n?x) to the beginning of the system CLASSPATH. That way the CWD is the first searched.

    db

Similar Threads

  1. Replies: 1
    Last Post: 03-24-2011, 04:01 AM
  2. Replies: 3
    Last Post: 08-25-2010, 11:21 AM
  3. Replies: 1
    Last Post: 06-30-2009, 03:32 PM
  4. output "left click"/enter command?
    By Arsenic in forum New To Java
    Replies: 2
    Last Post: 06-04-2009, 06:01 AM
  5. Replies: 1
    Last Post: 10-20-2008, 08:35 AM

Posting Permissions

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