How do you set java version in windows git bash?

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.


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.


  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

(Windows 10)