How do you set java version in windows git bash?

Viewed 61784

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...

5 Answers

Let's say you want to try other Java versions but you don't want to alter the Windows environment variables.

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.

Yes the environment variables affect which java is used in Git Bash. But if setting the $JAVA_HOME and $PATH variables doesn't seem work, try this:

  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.

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

Go the location where you want to set java path temporarily. Run below command. export PATH="/c/Program Files/Java/jdk-11.0.1/bin:$PATH"

User the jdk version you want in place of jdk-11.0.1

Related