I have a Windows 7 system on which I have installed the latest Java compiler. I also have the latest Cygwin. I want to use the Java compiler from Cygwin's shell. I edited the PATH variable i开发者_如何学Pythonn Cygwin as follows:
export PATH=$PATH:"/cygdrive/C/Program\ Files/Java/jdk1.6.0_23/bin/"
I can see the javac
binary in the above directory, however when I try to compile my *.java file I get:
javac command not found
Am I doing something wrong in setting the PATH variable like this? Do I have to do something else? I am new to Java and not very familiar with cygwin.
as you write the it with double-quotes, you don't need to escape spaces with \
export PATH=$PATH:"/cygdrive/C/Program Files/Java/jdk1.6.0_23/bin/"
of course this also works:
export PATH=$PATH:/cygdrive/C/Program\ Files/Java/jdk1.6.0_23/bin/
Java binaries may be under "Program Files" or "Program Files (x86)": those white spaces will likely affect the behaviour.
In order to set up env variables correctly, I suggest gathering some info before starting:
- Open DOS shell (type cmd into 'RUN' box) go to C:\
- type "dir /x" and take note of DOS names (with ~) for "Program Files *" folders
Cygwin configuration:
go under C:\cygwin\home\, then open .bash_profile and add the following two lines (conveniently customized in order to match you actual JDK path)
export JAVA_HOME="/cygdrive/c/PROGRA~1/Java/jdk1.8.0_65"
export PATH="$JAVA_HOME/bin:$PATH"
Now from Cygwin launch
javac -version
to check if the configuration is successful.
To bring more prominence to the useful comment by @johanvdw:
If you want to ensure your your javac file path is always know when cygwin starts, you may edit your .bash_profile
file. In this example you would add export PATH=$PATH:"/cygdrive/C/Program Files/Java/jdk1.6.0_23/bin/"
somewhere in the file.
When Cygwin starts, it'll search directories in PATH and this one for executable files to run.
If you are still finding that the default wrong Java version (1.7) is being used instead of your Java home directory, then all you need to do is simply change the order of your PATH variable to set JAVA_HOME\bin before your Windows directory in your PATH variable, save it and restart cygwin. Test it out to make sure everything will work fine. It should not have any adverse effect because you want your own Java version to override the default which comes with Windows. Good luck!
Although all other answers are technically correct, I would recommend you adding the custom path to the beginning of your PATH, not at the end. That way it would be the first place to look for instead of the last:
Add to bottom of ~/.bash_profile
:
export PATH="/cygdrive/C/Program Files/Java/jdk1.6.0_23/bin/":$PATH
That way if you have more than one java
or javac
it will use the one you provided first.
精彩评论