Switch between multiple java versions
While installing Android Studio on Ubuntu 14.04 I get the message that my Java version ( javac 1.7.0_79 ) is causing problems. I found a solution of how to install a newer Oracle version of Java:
sudo apt-add-repository ppa:webupd8team/java sudo apt-get update sudo apt-get install oracle-java8-installer
However I’m afraid that this might overwrite my existing open-jdk version of Java. Since I don’t know which of my programs depend on Java, I fear that this could crash these other programs. Is there a way to make sure apt-get doesn’t overwrite my previous Java? I would basically like to have installed both and be able to switch between them manually, depending on what version I need.
6 Answers 6
Apt-get won’t overwrite the existing java versions.
To switch between installed java versions, use the update-java-alternatives command.
update-java-alternatives --list
Set java version as default (needs root permissions):
sudo update-java-alternatives --set /path/to/java/version
. where /path/to/java/version is one of those listed by the previous command (e.g. /usr/lib/jvm/java-7-openjdk-amd64 ).
update-java-alternatives is a convenience tool that uses Debian’s alternatives system ( update-alternatives ) to set a bunch of links to the specified java version (e.g. java , javac , . ).
tq this solve my problem too. @Nenotlep what I did is edit the environment variable manually either in .bashrc or /etc/environment
@Nenotlep in order to change JAVA_HOME you have to run the following command: source /etc/environment
@janb I honestly don’t understand why this would have any effect unless you manually specified JAVA_HOME in /etc/environment . I just switched back and forth between java 8 and 11, and there is still no JAVA_HOME in /etc/environment . In fact, it is set nowhere in my environment. I therefor rejected the suggested edit. If there are circumstances in which the suggested command is needed, please suggest a new edit and explain when and why it would be necessary.
My $JAVA_HOME is unaffected by any of the proposed commands, too. My ~/.bashrc has a line export JAVA_HOME=$(readlink -f /usr/bin/javac | sed «s:bin/javac::») though and that does the trick for me: source ~/.bashrc . If your ~/.bashrc doesn’t have that line, run directly export JAVA_HOME=$(readlink -f /usr/bin/javac | sed «s:bin/javac::») .
sudo update-alternatives --config java
which lists all installed versions with current active one marked and provides dialog to switch:
There are 3 choices for the alternative java (providing /usr/bin/java). Selection Path. ------------------------------------------------------------ 0 /usr/lib/jvm/java-9-oracle/bin/java. * 1 /usr/lib/jvm/java-7-oracle/jre/bin/java. 2 /usr/lib/jvm/java-8-oracle/jre/bin/java. 3 /usr/lib/jvm/java-9-oracle/bin/java. Press to keep. [*], or type selection number:
export JAVA_HOME="$(jrunscript -e 'java.lang.System.out.println(java.lang.System.getProperty("java.home"));')"
to set $JAVA_HOME from current active version
As noted by @danzel at the accepted answer, this affects only the java executable, not the other parts of the configuration. Use update-java-alternatives if available.
Configuring Java
You can configure which version is the default for use in the command line by using update-alternatives , which manages which symbolic links are used for different commands.
sudo update-alternatives --config java
The output will look something like the following.
There are 5 choices for the alternative java (providing /usr/bin/java). Selection Path Priority Status ------------------------------------------------------------ * 0 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 auto mode 1 /usr/lib/jvm/java-6-oracle/jre/bin/java 1 manual mode 2 /usr/lib/jvm/java-7-oracle/jre/bin/java 2 manual mode 3 /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 1081 manual mode 4 /usr/lib/jvm/java-8-oracle/jre/bin/java 3 manual mode 5 /usr/lib/jvm/java-9-oracle/bin/java 4 manual mode Press to keep the current choice[*], or type selection number:
You can now choose the number to use as a default. This can also be done for other Java commands, such as the compiler ( javac ), the documentation generator ( javadoc ), the JAR signing tool ( jarsigner ), and more. You can use the following command, filling in the command you want to customize.
sudo update-alternatives --config command
Setting the JAVA_HOME Environment Variable
Many programs, such as Java servers, use the JAVA_HOME environment variable to determine the Java installation location.
Copy the path from your preferred installation and then open /etc/environment using Sublime Text or your favourite text editor.
At the end of this file, add the following line, making sure to replace the highlighted path with your own copied path.
JAVA_HOME="/usr/lib/jvm/java-8-oracle"
Save and exit the file, and reload it: source /etc/environment .
You can now test whether the environment variable has been set by executing the following command: echo $JAVA_HOME . This will return the path you just set.
Switch JDK version in Windows 10 cmd
Is there a way to change JDK version easily in cmd? like the version on mac. Change Default JDK on Mac.
7 Answers 7
Here’s my guide for Windows 10.
Step 1. Go to System Properties. Click on Environment Variables
Step 2. Add new variables, such as JAVA_8_HOME
- JAVA_8_HOME : %ProgramFiles%\Java\jdk1.8.0_152\bin
- JAVA_9_HOME : %ProgramFiles%\Java\jdk-9.0.1\bin
- JAVA_HOME : %JAVA_8_HOME%
In my case, JAVA_8_HOME (JDK8) is pointing to C:\Program Files\Java\jdk1.8.0_152\bin . You can replace this with your own path to javac . %JAVA_HOME% has a default value pointing to JAVA_8_HOME , which is the path for JDK8. That’s my preference, feel free to adjust accordingly.
Step 3. Select PATH and click on Edit . PATH
Step 4. Click on New and add %JAVA_HOME%. %JAVA_HOME% will be added to PATH automatically every time you launch a command prompt.
In order to switch JDK version in cmd, here’s the trick. Step 5. I created a batch file with
@echo off :: Switch JDK version DOSKEY java8=SET PATH=%JAVA_8_HOME%;%PATH%; DOSKEY java9=SET PATH=%JAVA_9_HOME%;%PATH%
Basically, it disables echo and creates two alias. In batch file any string after :: is the comments. Every time, java8 or java9 is called, it re-exports %PATH% with the new JDK path. Save it as profile.bat . You can name it whatever you want.
Step 6. Search for regedit (Registry Editor). Click on Edit > New > String Value . Give AutoRun as the Value name and %USERPROFILE%\profile.bat as the Value data . Here, please put your actual path value to the profile.bat we just created. So, whenever a command prompt is opened, it automatically loads profile.bat , which creates those two alias in the script.
Step 7. Close any command prompt you’re using or just open a new command prompt. This is because your changes will not affect opened cmd window. Environment changes only happens to new CMD.
Step 8. Verify your results here.
If you’re using different Python versions, same trick applies, too. Find my python environment settings here.
How can I change the Java Runtime Version on Windows (7)?
How can I change the Java Runtime Version on Windows. I installed Java 7 for some tests, and now I need the old java6 as system default, but I don’t want to uninstall the Java 7 (I need it for later tests). Can I change the system-used JRE in the control panel/Java/JRE tab? I can change/edit/add/delete the user-used version, but not the system-used.
8 Answers 8
For Java applications, i.e. programs that are delivered (usually) as .jar files and started with java -jar xxx.jar or via a shortcut that does the same, the JRE that will be launched will be the first one found on the PATH .
If you installed a JRE or JDK, the likely places to find the .exe s are below directories like C:\Program Files\JavaSoft\JRE\x.y.z . However, I’ve found some «out of the box» Windows installations to (also?) have copies of java.exe and javaw.exe in C:\winnt\system32 (NT and 2000) or C:\windows\system (Windows 95, 98). This is usually a pretty elderly version of Java: 1.3, maybe? You’ll want to do java -version in a command window to check that you’re not running some antiquated version of Java.
You can of course override the PATH setting or even do without it by explicitly stating the path to java.exe / javaw.exe in your command line or shortcut definition.
If you’re running applets from the browser, or possibly also Java Web Start applications (they look like applications insofar as they have their own window, but you start them from the browser), the choice of JRE is determined by a set of registry settings:
Key: HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Runtime Environment Name: CurrentVersion Value: (e.g.) 1.3
More registry keys are created using this scheme:
(e.g.) HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Runtime Environment\1.3 HKEY_LOCAL_MACHINE\Software\JavaSoft\Java Runtime Environment\1.3.1
i.e. one for the major and one including the minor version number. Each of these keys has values like these (examples shown):
JavaHome : C:\program Files\JavaSoft\JRE\1.3.1 RuntimeLib : C:\Program Files\JavaSoft\JRE\1.3.1\bin\hotspot\jvm.dll MicroVersion: 1
. and your browser will look to these settings to determine which JRE to fire up.
Since Java versions are changing pretty frequently, there’s now a «wizard» called the «Java Control Panel» for manually switching your browser’s Java version. This works for IE, Firefox and probably others like Opera and Chrome as well: It’s the ‘Java’ applet in Windows’ System Settings app. You get to pick any one of the installed JREs. I believe that wizard fiddles with those registry entries.
If you’re like me and have «uninstalled» old Java versions by simply wiping out directories, you’ll find these «ghosts» among the choices too; so make sure the JRE you choose corresponds to an intact Java installation!
Some other answers are recommending setting the environment variable JAVA_HOME . This is meanwhile outdated advice. Sun came to realize, around Java 2, that this environment setting is
- unreliable, as users often set it incorrectly, and
- unnecessary, as it’s easy enough for the runtime to find the Java library directories, knowing they’re in a fixed path relative to the path from which java.exe or javaw.exe was launched.
There’s hardly any modern Java software left that needs or respects the JAVA_HOME environment variable.
. and some useful information on multi-version support: