Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[JAVA] I seem to always need to enter the class path to run the program

  1. Dec 8, 2012 #1

    I wanted to try to use cmd to compile and run a program in java. I have sucessfully compiled it. The compiler is working properly (C:\new> javac output the compilers functions), but I'm always getting the same error: "Error: Could not find or load main class hello".

    When I specify the class path ("java -cp C:\new hello") the program works.

    How to solve this dilemma to succesfully run the programms in cmd?

    P.S. the source file name "hello.java", the class file name is "hello.class" and the specified class is "hello". Even though only the <>.class and the class name itself has to match I wanted to make sure everything is in tact there.

    Help realy needed here.
    Last edited: Dec 8, 2012
  2. jcsd
  3. Dec 8, 2012 #2
    Current directory is not part of the class path by default. You can edit your environment variables and add "." to the classpath (warning: unsafe on 'real' systems).
  4. Dec 8, 2012 #3
    Sorry, but what do you mean by "You can edit your environment variables and add "." to the classpath (warning: unsafe on 'real' systems)"

    1. What do you mean by "."
    2. And "unsafe for "real" systems?

    P.S. I edited the environmental variables. It wokrs fine.
  5. Dec 8, 2012 #4
    "." is the name of your current directory, e.g. if you're in "c:\new" then "." == "c:\new".

    Servers out in the world, accessible by the public, or private multiuser systems. In these systems having "." in paths is dangerous because if you're in another directory that is writable by other people, someone could put a program there called "java" and when you say "java hello" it could run their program and not the real java application -- or they could put a "hello.java" in that directory and it would run that instead of yours.

    It's always better (read: safer) to always specify the full path to applications (like java / java.exe) and to always specify the classpath with -cp, than it is to change search paths.
  6. Dec 8, 2012 #5
    Thank you
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook