Search code examples
javacmdjavac

I can't set a permanent path in Java


I recently created my very first Java file and tried to compile it in cmd, but whenever I do so, I have to set the specific path to the file. After setting the right path to the file, javac works properly. However, this is very annoying and time-consuming for me. I did some research about it and I presume that if I set a permanent path in the 'system variables', then I will never have to set the same path again.

However, even after I set the path of the file's location (the location is correct) in the system variables, javac still returns "file not found".


Solution

  • javac cannot do what you want, not with 'system variables' as you describe. But this isn't a problem java programmers normally run into:

    1. If you really want to compile on the command line with a shell, ooookay, that's not the normal way to do it (that's to use smart editors and build systems), but.. just have a terminal open, in the right directory (the one containing 'YourApp.java'), and you can just type javac YourApp.java, and that will work.

    2. If your project is more complicated and you insist on javac on the command line, make a shell script.

    3. More usually, you have a smart editor, such as Eclipse or Intellij. Even if you don't, most 'dumb' editors still have a menu option to run a command on the current file. Add an entry for javac and voila, you're now 1 key combo away from compiling the code.