windows 你如何在windows git bash中设置java版本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11723442/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you set java version in windows git bash?
提问by Lieven Cardoen
How do you set the java version to use in a windows git bash? Does it work via the environment variables? Java -version gives another version in dos command prompt than in git bash...
你如何设置java版本在windows git bash中使用?它是否通过环境变量工作?Java -version 在 dos 命令提示符中提供了另一个版本而不是在 git bash 中...
采纳答案by dsh
You don't set the java version. You are seeking the $PATH
(bash) and the %PATH%
(Windows/DOS/cmd.exe) environment variables. When you run the command java -version
, the shell searches the directories on the $PATH
to locate the java
program and run it with the given arguments. If you get different results in different shells then that means you have different PATHs such that a different installation of Java is found.
你没有设置java版本。您正在寻找$PATH
(bash) 和%PATH%
(Windows/DOS/cmd.exe) 环境变量。当您运行命令时java -version
,shell 会搜索 上的目录$PATH
以定位java
程序并使用给定的参数运行它。如果您在不同的 shell 中得到不同的结果,那么这意味着您有不同的 PATH,从而可以找到不同的 Java 安装。
回答by Kim G.
Let's say you want to try other Java versions but you don't want to alter the Windows environment variables.
假设您想尝试其他 Java 版本,但不想更改 Windows 环境变量。
Steps:
脚步:
- Go to your home folder. i.e. C:\Users\MyAccountName
Edit the .bashrc file and add the following code below.
#For example: export JAVA_HOME='/c/Program Files (x86)/Java/jdk1.8.0_45' export PATH=$JAVA_HOME/bin:$PATH
.bashrc and .bash_profile are files created during the git-bash installation.
- 转到您的主文件夹。即 C:\Users\MyAccountName
编辑 .bashrc 文件并在下面添加以下代码。
#For example: export JAVA_HOME='/c/Program Files (x86)/Java/jdk1.8.0_45' export PATH=$JAVA_HOME/bin:$PATH
.bashrc 和 .bash_profile 是在 git-bash 安装期间创建的文件。
回答by cykedelielic
Yes the environment variables affect which java is used in Git Bash. But if setting the $JAVA_HOME and $PATH variables doesn't seemwork, try this:
是的,环境变量会影响 Git Bash 中使用的 Java。但是如果设置 $JAVA_HOME 和 $PATH 变量似乎不起作用,试试这个:
- Go to the folder where Git Bash is installed (usually C:\Program Files\Git)
- Go to usr/bin folder, i.e. C:\Program Files\Git\usr\bin
- There should be a file named "java" there. Rename it to "notjava".
Now set the PATH variable as described in the other answers. I.e.
export JAVA_HOME='{yourjavahome}'
Restart Git Bash.
- 转到安装 Git Bash 的文件夹(通常是 C:\Program Files\Git)
- 转到 usr/bin 文件夹,即 C:\Program Files\Git\usr\bin
- 那里应该有一个名为“java”的文件。将其重命名为“notjava”。
现在按照其他答案中的描述设置 PATH 变量。IE
导出 JAVA_HOME='{你的javahome}'
重新启动 Git Bash。
This is what worked for me. I'm using Git for Windows v2.17.0.
这对我有用。我在 Windows v2.17.0 上使用 Git。
回答by andydavies
- Go to System Properties -> Advanced -> Environment Variables
- New System Variable
- 'Variable name': PATH
- 'Variable value': C\Program Files\Java\jdk-9\bin (or whatever your path is)
- Restart Git Bash
- 转到系统属性 -> 高级 -> 环境变量
- 新系统变量
- '变量名':PATH
- '变量值':C\Program Files\Java\jdk-9\bin(或任何你的路径)
- 重启 Git Bash
(Windows 10)
(Windows 10)