As an Android developer, there may be multiple apps where we work on the same computer. Apps from work, personal projects, samples or maybe we are working on some codelabs.
One of the things they have in common is that they all need the Java Development Kit (JDK) to be installed. Luckily for us, Android Studio has it bundled so we do not need to install it.
Sometimes we do not only work on Android projects, we may have some backend projects too. For Android we currently use JDK 1.8, but that does not mean other projects cannot use newer versions of Java.
I recently started working on another project where we use Java 11 alongside Java 8. That means for some repositories we use Java 11 and for others Java 8.
For this tutorial, we will assume that we already have %JAVA_HOME%\bin
in our Path
environment variable.
Introducing a second JDK
I downloaded the OpenJDK 11 and installed it.
Everything worked nicely when using IntelliJ IDEA as you can configure which JDK you want the IDE to use.
Then I tried to run a command for my Java 11 project on the terminal and I got this error:
> invalid source release: 11
Next I went to check my environment variables on Windows.
My JAVA_HOME
points to C:\Program Files\Android\Android Studio\jre
.
OK, it makes sense.
JAVA_HOME
still points to Java 8 folder, that’s why it failed.
How can I choose which Java version should the command line execute? 🤔
Switch Java version on demand
Ideally, we would have both Java folders in the environment variable.
However, this would be no help has some files like java.exe
or javac.exe
exist on both folders, so a clash would happen.
That leaves us with one way to go, switch such variable on demand to specify which JDK we want to use. Let’s start the setup.
Create a folder and add it to environment variables
- Create a folder called
Scripts
and place it somewhere where it will permanently stay. - Copy the path of the folder. Should be something like:
C:\...\Scripts
- Go to
Control Panel > System
, then on the right side (Windows 10) go toAdvanced system settings
. Alternatively we can go there faster by opening the start menu and typingenv
. - Open the
Environment variables
settings. - Select the
Path
entry in theSystem variables
section below and press theEdit...
button. - In the new window press on
New
, paste the path that we copied and close the window saving the changes.
Create scripts to set JAVA_HOME environment variable
In this case we only have Java 8 and Java 11. If we would have more versions, we would just repeat the process to create as many scripts as we need.
In our newly created folder, add a new file java8.bat
:
@echo off
echo "Setting Java 8 as JAVA_HOME as system environment variable (script must be run as administrator)"
setx JAVA_HOME "C:\Program Files\Android\Android Studio\jre" /M
echo Java 8 activated.
Similarly, add a new file java11.bat
:
@echo off
echo "Setting Java 11 as JAVA_HOME as system environment variable (script must be run as administrator)"
setx JAVA_HOME "C:\Program Files\java\jdk-11.0.2" /M
echo Java 11 activated.
Note: remember to change the path for every java version in the scripts!
Run scripts as administrator
As we may notice, the script needs to be run as an administrator.
The reason for this is that we want this variable to be set at system level and not at user level.
That will completely override the already existent JAVA_HOME
from the system environment variables.
If we want the scripts to add the variable at user level, they would look like this for java8.bat
and java11.bat
respectively:
@echo off
echo "Setting Java 8 as JAVA_HOME as user environment variable"
set JAVA_HOME=C:\Program Files\Android\Android Studio\jre
echo Java 8 activated.
@echo off
echo "Setting Java 11 as JAVA_HOME as user environment variable"
set JAVA_HOME=C:\Program Files\java\jdk-11.0.2
echo Java 11 activated.
In my case, I wanted to have it at system level so that my GIT hooks could automatically use the right version.
Summary
That is it! We can now easily switch between Java versions very easily from any directory:
C:\>java8
"Setting Java 8 as JAVA_HOME as system environment variable (script must be run as administrator)"
SUCCESS: Specified value was saved.
Java 8 activated.
C:\>java11
"Setting Java 11 as JAVA_HOME as system environment variable (script must be run as administrator)"
SUCCESS: Specified value was saved.
Java 11 activated.
Note: when we run java -version
right after calling one of our scripts it may still show the old value.
Close the terminal and open it again so that it takes the new value.
Thanks for reading!
If you found this article interesting, share it!