Reputation: 2915
I have three different JDKs on my system so I can appropriately switch between projects. Importing the appropriate JDK in my IDE is no issue, but the command line is. See, despite the fact that JAVA_HOME
for both my user as well as the system point to jre1.8.0_261
:
java --version
seems to still think that my default is Amazon's Corretto JDK, which I only need for one specific project:
C:\Users\jasonfil>java --version
openjdk 11.0.8 2020-07-14 LTS
OpenJDK Runtime Environment Corretto-11.0.8.10.1 (build 11.0.8+10-LTS)
This is also true for Powershell. I have also tried in both cmd
and Powershell as administrator. Same issue.
Curiously, when I query the environment variable from cmd
, I get the intended JDK:
C:\Users\jasonfil>echo %JAVA_HOME%
C:\Program Files (x86)\Java\jre1.8.0_261
While on Powershell, echo
just outputs the string %JAVA_HOME
literally:
PS C:\Users\jasonfil> echo %JAVA_HOME%
%JAVA_HOME%
My goal is to be able to switch between Java versions at will, like the update-alternatives
mechanism does in UNIX-based systems. Any ideas?
Upvotes: 1
Views: 9839
Reputation: 561
I added the environment variable and then restarted my machine worked well.
Upvotes: 0
Reputation: 191
If the PATH environment variable contains an entry for each java installation on your machine, then the console is going to always use the java installation which occurs first in the PATH environment variable. If you want to use a specific version of java, modify the path environment variable so that version of java's bin folder is declared before any of the other installed versions of java.
Upvotes: 0
Reputation: 159096
Java doesn't use the JAVA_HOME
environment variable. Some tools do, especially during installation of the tools, but that's different.
Java doesn't even need to be on the PATH
, but it is certainly more convenient if it is.
The question shows that there is a Java installed at C:\Program Files (x86)\Java\jre1.8.0_261
.
It also shows that there is an OpenJDK 11.0.8 somewhere, but doesn't show where, so for this answer, we'll pretend it was installed/unzipped to C:\foo\openjdk-11.0.8
.
The java
executable is in the bin
folder, so to show that the PATH
and JAVA_HOME
doesn't matter, run the java
command fully qualified. On my machine, I get the following outputs (with different actual paths and I have different versions, but whatever):
C:\>"C:\Program Files (x86)\Java\jre1.8.0_261\bin\java" -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
C:\>C:\foo\openjdk-11.0.8\bin\java -version
openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)
Running java
unqualified is of course easier, so add the bin
folder of Java you want to the front of the PATH
. You can even do that in a command prompt to change the "default" Java for that command prompt without affecting your "global default" as specified in that environment property dialog.
You can keep doing it in the same command prompt, if needed, to flip back and forth, but you'd want to limit that, since the PATH
will get longer and longer, with lots of redundant entries, the more you do it.
C:\>set PATH=C:\Program Files (x86)\Java\jre1.8.0_261\bin;%PATH%
C:\>java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)
C:\>set PATH=C:\foo\openjdk-11.0.8\bin\java\bin;%PATH%
C:\>java -version
openjdk version "11.0.2" 2019-01-15
OpenJDK Runtime Environment 18.9 (build 11.0.2+9)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)
Upvotes: 3
Reputation: 79075
All the outputs you are getting are as expected.
JAVA_HOME
has nothing to do with the Java version. It is simply a variable that conventionally most of the Java-based software (e.g. Web & App servers, IDEs etc.) use to refer to find the JDK installation. Therefore, you should set it to the base folder of JDK e.g. C:\Program Files\Java\jdk1.8.0_261
.java -version
looks for java.exe
which is inside the bin
folder of the JDK installation e.g. C:\Program Files\Java\jdk1.8.0_261\bin
. Therefore, C:\Program Files\Java\jdk1.8.0_261\bin
should be one of the values in your PATH
variable. If you have already set JAVA_HOME
as mentioned above, setting up the path of java.exe
becomes easier as you can set %JAVA_HOME%\bin
instead of C:\Program Files\Java\jdk1.8.0_261\bin
in your PATH
variable.echo %JAVA_HOME%
will return you the value set in your JAVA_HOME
variable. Similarly, if you use echo %PATH%
, you will get the values set in your PATH
variable.Note: You are not provided with a JRE starting with Java-11.
Upvotes: 3
Reputation: 11
Both cmd and Powershell will reference Path variable to check if the requested executable is in the PATH. The JAVA_HOME variable will only be used by the IDE or the Java applications. So to have the desired JDK in the path, append the JDK path to Path environment variable to either User variables (if you desire this settings should only afffect the current logged in user i.e. your login) or System Variable.
After you add these settings make sure you reopen cmd or Powershell console for the settings to take effect.
Upvotes: 0