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...
-
How have you fixed this ?Thermech– Thermech2013-11-09 23:36:18 +00:00Commented Nov 9, 2013 at 23:36
-
4I had PATH and JAVA_HOME set correctly, yet Git Bash kept on showing an older Java version. I'm running a 64 bit Win 7 and the catch was that I had an older 32 bit version on path 'C:\Program Files (x86)\Java\jre7\bin'. For some reason it got picked up by Git Bash although it was not explicitly referenced from anywhere.Mareen– Mareen2014-03-11 08:28:16 +00:00Commented Mar 11, 2014 at 8:28
-
Same issue here. On a "normal" command line prompt, JAVA_HOME points to the jdk8. In Git Shell, if I compile an android project for example, it complains JAVA_HOME points to the jre8.Thomas– Thomas2014-09-06 05:50:34 +00:00Commented Sep 6, 2014 at 5:50
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.
1 Comment
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.
2 Comments
man bash for details on how bash handles startup configuration.export PATH="/c/Program Files/Java/<jdk>/bin:$PATH"- 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
(Windows 10)
1 Comment
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:
- 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.
This is what worked for me. I'm using Git for Windows v2.17.0.
Comments
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