Changing current version of Java within Windows

15

7

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

Dunc

Posted 2011-03-24T15:04:17.937

Reputation: 2 150

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. – Rekin – 2011-03-24T15:08:16.980

Answers

21

Change your PATH variable so that it has the location of the jdk5/bin directory:

  1. Start -> Control Panel -> System -> Advanced
  2. Click on Environment Variables, under System Variables, find PATH, and click on it.
  3. 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.
  4. Close the window.
  5. Reopen Command prompt window, and run java -version

dogbane

Posted 2011-03-24T15:04:17.937

Reputation: 3 771

3If 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. – Ayusman – 2014-06-24T17:32:53.557

That didn't work. The jre bin wasn't previously part of the path so it must be set somewhere else. – Dunc – 2011-03-24T15:14:17.600

9That'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. – dogbane – 2011-03-24T15:17:48.093

Sorry didn't spot the beginning part. Thanks – Dunc – 2011-03-24T15:25:46.797

15

In the command shell:

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.

Mike Thomsen

Posted 2011-03-24T15:04:17.937

Reputation: 251

6

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.

Ron

Posted 2011-03-24T15:04:17.937

Reputation: 161

1Well, you can always place your old Java location in the PATH, in the first place, and it will win. – leonbloy – 2015-01-10T00:32:14.397

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.

– None – 2016-01-12T01:01:29.630

3

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)

Vidar Johansen

Posted 2011-03-24T15:04:17.937

Reputation:

1

The latest version of JRE that you have always takes precedence over any PATH setting. So, to be sure, uninstall the 1.6 JRE if you don't want it to be the main one. You can have any number of JDKs installed in parallel.

adarshr

Posted 2011-03-24T15:04:17.937

Reputation: 131

1

Since Java supports a -version command line option, you can use it to select a specific version to run, e.g.:

java -version:1.7 -jar [path to jar file]

will run a jar application in Java 1.7, if it is installed.

For further details, see Oracle's documentation.

Akhilesh Dubey

Posted 2011-03-24T15:04:17.937

Reputation: 111

@Jawa this java -version:1.7 -jar [path to jar file] is no longer supported. You might need to go the PATH route Regards. – Adeola Ojo – 2018-04-04T13:00:19.503

0

Create a “setjava.bat” file and save this file with below content and do not forget to change JAVA_PATH for your system. Also, remember to save this file with .bat extension.

echo off
REM IMPORTANT
REM RUN THIS SCRIPT AS ADMINISTRATOR
set JAVA_REQ_VER=%1
IF "%JAVA_REQ_VER%" == "7" (
 set JAVA_PATH="C:\Progra~1\Java\jdk1.7.0_79"
) ELSE (
 REM At present I only need to assign 8 by default if not 7
 set JAVA_PATH="C:\Progra~1\Java\jdk1.8.0_111"
)
setx /M JAVA_HOME "%JAVA_PATH%"

Now you just run this script with administrator privileges.

Example: setjava 8, setjava 7

Check this link for more details

Ricky

Posted 2011-03-24T15:04:17.937

Reputation: 291