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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:44:51  来源:igfitidea点击:

How do you set java version in windows git bash?

windowsgit-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 $PATHto locate the javaprogram 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 变量似乎不起作用,试试这个:

  1. Go to the folder where Git Bash is installed (usually C:\Program Files\Git)
  2. Go to usr/bin folder, i.e. C:\Program Files\Git\usr\bin
  3. There should be a file named "java" there. Rename it to "notjava".
  4. Now set the PATH variable as described in the other answers. I.e.

    export JAVA_HOME='{yourjavahome}'

  5. Restart Git Bash.

  1. 转到安装 Git Bash 的文件夹(通常是 C:\Program Files\Git)
  2. 转到 usr/bin 文件夹,即 C:\Program Files\Git\usr\bin
  3. 那里应该有一个名为“java”的文件。将其重命名为“notjava”。
  4. 现在按照其他答案中的描述设置 PATH 变量。IE

    导出 JAVA_HOME='{你的javahome}'

  5. 重新启动 Git Bash。

This is what worked for me. I'm using Git for Windows v2.17.0.

这对我有用。我在 Windows v2.17.0 上使用 Git。

回答by andydavies

  1. Go to System Properties -> Advanced -> Environment Variables
  2. New System Variable
  3. 'Variable name': PATH
  4. 'Variable value': C\Program Files\Java\jdk-9\bin (or whatever your path is)
  5. Restart Git Bash
  1. 转到系统属性 -> 高级 -> 环境变量
  2. 新系统变量
  3. '变量名':PATH
  4. '变量值':C\Program Files\Java\jdk-9\bin(或任何你的路径)
  5. 重启 Git Bash

(Windows 10)

(Windows 10)