如何设置 Cygwin PATH 来查找 javac?
我有一个 Windows 7 系统,上面安装了最新的 Java 编译器。我还有最新的 Cygwin。我想使用 Cygwin shell 中的 Java 编译器。我在 Cygwin 中编辑了 PATH 变量,如下所示:
export PATH=$PATH:"/cygdrive/C/Program\ Files/Java/jdk1.6.0_23/bin/"
我可以在上面的目录中看到 javac
二进制文件,但是当我尝试编译我的 *.java 文件时,我得到:
javac command not found
我在设置PATH 变量是这样的吗?我还需要做其他事情吗?我是Java新手,对cygwin不太熟悉。
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 in 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
当你用双引号写它时,你不需要用
\
转义空格,当然这也有效:
as you write the it with double-quotes, you don't need to escape spaces with
\
of course this also works:
Java 二进制文件可能位于“Program Files”或“Program Files (x86)”下:这些空格可能会影响行为。
为了正确设置环境变量,我建议在开始之前收集一些信息:
Cygwin 配置:
转到 C:\cygwin\home\ 下,然后打开 .bash_profile 并添加以下两行(方便自定义,以便与您实际的 JDK 路径匹配)
现在从 Cygwin 启动
javac -version
来检查配置是否成功。
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:
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)
Now from Cygwin launch
javac -version
to check if the configuration is successful.
为了更加突出 @johanvdw 的有用评论:
如果您想确保 cygwin 启动时始终知道您的 javac 文件路径,您可以编辑
.bash_profile
文件。在此示例中,您将在文件中的某处添加export PATH=$PATH:"/cygdrive/C/Program Files/Java/jdk1.6.0_23/bin/"
。当 Cygwin 启动时,它将在 PATH 和此目录中搜索要运行的可执行文件。
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 addexport 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.
如果您仍然发现使用默认错误的 Java 版本 (1.7) 而不是 Java 主目录,那么您所需要做的就是更改 PATH 变量的顺序,将 JAVA_HOME\bin 设置在 Windows 目录之前。 PATH变量,保存并重新启动cygwin。进行测试以确保一切正常。它不应该产生任何不利影响,因为您希望自己的 Java 版本覆盖 Windows 附带的默认版本。祝你好运!
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!
尽管所有其他答案在技术上都是正确的,但我建议您将自定义路径添加到 PATH 的开头,而不是末尾。这样,它将是第一个要查找的位置,而不是最后一个:
添加到
~/.bash_profile
的底部:这样,如果您有多个
java
或 < code>javac 它将使用您首先提供的那个。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
:That way if you have more than one
java
orjavac
it will use the one you provided first.