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:
Changing current version of Java within Windows
I’m working in a Windows XP environment and have recently installed java 1.6 because it was required by an application. However I don’t want this to be the default version of java to be used. How do I set it so that the command java -version will return 1.5.x
I would take a look at path environment variable. You can examine it in command line with: echo %PATH% . These are default folders where binaries are looked for. If you find a path to JAVA you could adjust it easily in System Preferences.
7 Answers 7
Change your PATH variable so that it has the location of the jdk5/bin directory:
- Start -> Control Panel -> System -> Advanced
- Click on Environment Variables, under System Variables, find PATH, and click on it.
- In the Edit windows, modify PATH by adding the location of your jdk5/bin directory to the beginning. If you do not have the item PATH, you may select to add a new variable and add PATH as the name and the location of the directory as the value.
- Close the window.
- Reopen Command prompt window, and run java -version
That’s because when you install Java, the bins are put into C:\WINDOWS\system32, which is part of your PATH. You should add the jre/bin directory to the beginning of the PATH.
If you have multiple JREs installed, the only way to achieve this is by changing the registry entry. Especially because there are tools which read the JRE info from registry and not the class path.
set JAVA_HOME=C:\jdk1.6.0u24 set PATH=%JAVA_HOME%\bin;%PATH%
That will temporarily set up the environment in the command shell. Maven, Ant, etc. will pick up on your new version of Java without having to go to the Control Panel repeatedly.
Tools like Eclipse should be able to select which JDK to use in their own configuration tools for use within their environments.
Java 8 creates three shortcuts on \ProgramData\Oracle\Java\javapath that point to the latest Java8 java.exe, javaw.exe and javaws.exe and then puts \ProgramData\Oracle\Java\javapath at the front of the PATH so that no matter what you do to the JAVA_PATH environment variable, you still get the latest Java 8.
You can get around this by
1) renaming \ProgramData\Oracle\Java\javapath to something else (\ProgramData\Oracle\Java\javapath8 for example)
2) creating a new javapath folder under Java and
3) creating the shortcuts that you need.
Just restore the javapath when you are done with the old versions and you get Java 8 back.
Short, but to the point, very good answer, this one explains more specifically how to generate multiple versions of javapath folders, using symbolic links, so you could switch with no time between versions, and the registry stuff that is missing here, screenshots and trick to avoid reboot after changes too. see stackoverflow.com/questions/27996603.
There are two ways to fix this:
1) Change the PATH (as someone has already mentioned) The important thing with this solution is to set JAVA_HOME before the windows paths. This is because under the windows folder, there is a java.exe that redirects to the last installed jre.
2) Regedit. The key HKEY_LOCAL_MACHINE->SOFTWARE->JAVASOFT->Java Runtime Environment contains the last installed version that the java.exe in the windows folder redirects to. If you change this to a previously installed version, everything should be peachy. (At least, I think this is the right registry key)
Java on Windows: switching to a specific Java version/ runtime
This post show how to have multiple Java runtime environments on your PC and switch between them on Windows so that a particular program uses the version that you want it to.
Background
Due to Java SE Platform licensing changes by Oracle, developers have turned to alternatives such as OpenJDK. The AdoptOpenJDK site provides Prebuilt OpenJDK Binaries for Free (the main line on their landing page) and as you will see, there are numerous versions that you could use:
- JDK 8 Long-term Support ( LTS )
- JDK 11 LTS
- JDK 16 (the latest)
- Each of these could be 32-bit or 64-bit versions and you can choose the JVM that you run (Hotspot vs OpenJ9)
In short, there are options and there are times when you want to run something with a specific version of the JVM or you want to test something with multiple JVMs.
Installing and switching between multiple Java versions
The method that I follow is that I have a folder D:\apps where I put different applications tools that I want to use. Under this, I create a folder called jdk and use that as the main holding folder for all the different JDK / JVM that I want to use. I personally prefer not installin each JVM / JDK into my computer any more but to use them ‘without installation’.
Head on over to the releases page at AdoptOpenJDK to get the package you need. Start by changing the platform to Windows as shown below.
Next, pick the JDK (the full Java Development Kit) or the smaller JRE (Java Runtime Environment) and download the ZIP file version. Remember x86 means it’s the 32-bit version and x64 means that it’s the 64-bit version. If you have a modern (relatively new) Windows 10 PC, it’s almost certainly running the 64-bit version of Windows and will therefore support both the versions. In that case, the x64 is preferable though you may have some programs that don’t work correctly on the 64-bit version and need the 32-bit version.
Once you have downloaded the version (or versions) that you want to get, unzip the package. In my case, I unzip it to d:\apps\jdk so that I have all the JDKs/ JREs in one place. After unzipping, I change the folder names to be something like jdk-11.0.11_x64 and jdk-11.0.11_x86 so that I can easily tell which folder has the 64-bit and which one has the 32-bit.
Running a program with Java basically requires you to do: $ java . and for this to work, it needs two things to be in place:
- The Java version that you want to run must be the first Java installation on the path (so that Windows can find it)
- The environment variable JAVA_HOME must be set to the installation that you want to use
So, for switching Java versions, you need to do this:
set JAVA_HOME=d:\apps\jdk\jdk-11.0.11_x86\ path=d:\apps\jdk\jdk-11.0.11_x86\bin;%PATH%
The first line sets the JAVA_HOME and the second one moves the x86 version of the JDK / JRE to the path. Note that the JAVA_HOME points to the main directory where the whole installation is, and the path must be to the /bin folder under the JAVA_HOME so that java.exe can be found. After you do this, you should see this:
$ java -version openjdk version "11.0.11" 2021-04-20 OpenJDK Runtime Environment AdoptOpenJDK-11.0.11+9 (build 11.0.11+9) OpenJDK Client VM AdoptOpenJDK-11.0.11+9 (build 11.0.11+9, mixed mode)
To switch to a different version, you just need to set both those items correctly again. For example, in my case, I would do:
$ path=d:\apps\jdk\jdk-11.0.11_x64\bin;%PATH% $ set JAVA_HOME=d:\apps\jdk\jdk-11.0.11_x64\ $ java -version openjdk version "11.0.11" 2021-04-20 OpenJDK Runtime Environment AdoptOpenJDK-11.0.11+9 (build 11.0.11+9) OpenJDK 64-Bit Server VM AdoptOpenJDK-11.0.11+9 (build 11.0.11+9, mixed mode)
(Notice how the last line shows 64-Bit Server VM since it’s now running the 64-bit VM)
It’s not as convenient as using pik to switch Ruby versions on Windows but I imagine it would be a good idea to write something like pik for this. Also, I did not search if there are tools that let you do this easily but there might well be something.
That’s all there is to it. I noted this down mainly for myself since I need to do this occasionally. If it helps you, that’s great! Please leave a comment if there is something else that you think we should add into the post.