104

How do I persuade Windows to use the JDK instead of a JRE?

This question has been asked before, here and elsewhere:

How do I set the default Java installation/runtime (Windows)?

The trouble is that Windows ignores JAVA_HOME and it also ignores the fact that I made the JDK bin directory the first entry in the path.

When I run java -version from the command line, instead of invoking my JDK 1.6 installation, it runs the 1.7 JRE.

My guess is that this is a problem specific to 1.7, and Windows 7 is doing something it shouldn't with the registry.

Any ideas on how to fix this?

Edit: Oops. I wrote "first entry in the classpath" above, when I meant "path". Sorry.

MarianD
  • 13,096
  • 12
  • 42
  • 54
ccleve
  • 15,239
  • 27
  • 91
  • 157

19 Answers19

143

On Windows, the same java executable can load DLLs for different versions of Java. It looks at the directory from which it's running to see if it contains libraries for a particular version of Java. If not, it uses the Windows Registry to locate the default version of Java for the system.

The Java installer will put a copy of java.exe (but no libraries) in the the Windows\system32 C:\Program Files (x86)\Common Files\Oracle\Java\javapath directory, and add that directory to the beginning of the PATH variable.

If you don't use a full path, the copy of java.exe to run is found by using the PATH system variable. Since this directory doesn't contain the DLLs of a particular Java runtime version, one is located one by looking at the registry.

So, you either need to modify the registry, or replace the javapath entry with the version of Java you want in your PATH system (not user) variable.

erickson
  • 265,237
  • 58
  • 395
  • 493
  • 11
    OP here. See my edit above. Yup, that solved it. Two notes: first, it appears that the Java installer really did put java.exe in Windows\System32. This is astounding to me. Second, when you modify the path under Computer/Properties/Advanced System Settings/Environment Variables, you have to modify the **system** path, not the **user** path. The system path puts itself first. – ccleve Mar 31 '11 at 17:00
  • 2
    I will also underscore user237815's point that it seems the Java installer actually drops its java.exe into Windows\System32. This certainly was not intuitive for me. Only after checking the "java -version" from a console did I gain any comfort level that Windows was now using the java.exe I wanted from my JDK installation rather than some previous and undesired version. – John Tobler Jan 25 '12 at 20:22
  • 19
    The issue was also resolved in my case by renaming the java.exe, javaw.exe, and javaws.exe in my Windows/System32 folder. Then, and only then, does java -version show the version that is set in JAVA_HOME and the PATH – Inversus Sep 25 '12 at 14:20
  • 6
    You saved my day! Moving the entry related to the JDK before Windows in PATH solved the issue. Thank you so much! – the_dark_destructor Jan 10 '13 at 16:00
  • 2
    wow this was so annoying to track down, thank you Inversus! deleting the java executables in windows\system32 solved it for me. – magritte Jun 13 '14 at 16:55
  • I installed java 8 for experimental purposes but again it hijacked my general purpose machine :( – prusswan Sep 03 '14 at 14:05
  • `put the version of Java you want before the Windows directory in your PATH.` worked like charm. – Dileepa Sep 21 '15 at 12:22
  • Thousand thanks for this solution. I was fedup of installing and uninstalling java versions for checking compatibility. – Babanna Duggani Jan 28 '16 at 14:21
  • 16
    I put `%JAVA_HOME%\bin` in first position of `path` variable on Windows 10 and work to me. – Wendel May 31 '16 at 19:01
  • 1
    for java 8, it doesn't place java exes in c:/windows/system32 anymore. – Broken_Window May 31 '17 at 16:01
  • After setting JAVA_HOME and PATH (in both system and user) my PATH wasn't updated until I restarted. I'm on Windows 10. Also, I found no java executables in System32. But, my issue was resolved. – Joshua M Jul 03 '17 at 20:02
41

In Windows 8, you may want to remove C:\ProgramData\Oracle\Java\javapath directory.

from path

It solved my issue.

shareef
  • 9,255
  • 13
  • 58
  • 89
Arun
  • 511
  • 4
  • 3
  • 6
    In windows 10, its: C:\Program Files (x86)\Common Files\Oracle\Java\javapath. This get automatically added to System Path variable. Delete that ans set your custom path location. – Dexter Sep 16 '19 at 12:27
  • 3
    the above comment helped in case of windows 10 – Saba Ahang Jun 30 '20 at 14:35
26

I have this issue too. I am running 1.6 but want to build the code I'm working on with 1.5. I've changed the JAVA_HOME and PATH (both user and system) to no avail.

The answer is that the installer for 1.6 dropped java.exe, javaw.exe, and javaws.exe into my Windows\System32 folder (Windows 7).

I solved it by renaming those files to java_wrong.exe, javaw_wrong.exe, and javaws_wrong.exe. Only after doing that does it pick up the correct version of java as defined in JAVA_HOME and PATH. I renamed the files thusly because that deleted them in an easily reversible manner.

Hope this helps!

MarianD
  • 13,096
  • 12
  • 42
  • 54
Inversus
  • 3,125
  • 4
  • 32
  • 37
  • 11
    Instead of renaming, you could modify the PATH variable specifying as first value %JAVA_HOME%\bin, that way it would be the first value it will take – maxivis Jan 07 '13 at 13:35
  • 1
    @maxivis Thanks. I guess the issue might have been the ordering of PATH elements such that Windows\System32 was before JAVA_HOME. I simply renamed them so that they would be effectively deleted in a reversible way. – Inversus Jan 21 '17 at 19:09
  • Somehow setting JAVA_HOME in path was not working in any way for me, this one actually solved it. – Guilherme Orioli Oct 30 '18 at 13:49
  • The problem was not resolved until I restarted the computer after the changes. – G. Ciardini Apr 30 '21 at 08:37
12

Windows doesn't ignore anything. This is an issue with your setup; Windows just uses what you provide. It has no special knowledge of JAVA_HOME.

CLASSPATH has nothing to do with Windows, either. To Windows it's only an environmental variable that gets expanded to a folder location.

Check your %PATH% environmental variable. It's what's making Windows find one before the other. The path (as the post you linked to said) should point to %JAVA_HOME%\bin;<remainder of path>. Again, the post you linked to gave you a way to set this using a batch file.

(For others who might not know this: The easiest way to inspect the %PATH% is to open a command prompt and type echo %PATH%. You can also get there by right-clicking on Computer in the right pane of the Start Menu and choosing Properties, then Advanced System Settings, and the tne Environmental Variables button.)

Ken White
  • 123,280
  • 14
  • 225
  • 444
  • @erickson: OK. Thanks - I'll correct that. It still has nothing to do with Windows, however, which was the point of my answer. :) – Ken White Mar 30 '11 at 22:24
  • Understood, I was just concerned that a *new* misconception could be created; Java doesn't know anything more about `JAVA_HOME` than Windows does. – erickson Mar 30 '11 at 22:28
  • I appreciate the correction. I'm not a Java person myself (limited experience to date), so it's good to know for future reference. – Ken White Mar 30 '11 at 22:30
  • 1
    `JAVA_HOME` should be set to the root of the java installation. The path should have `%JAVA_HOME%\bin;<remainder of path%gt;`, otherwise you'll wind up with not "finding" java, javac, etc. Of course, Windows only really looks at the `PATH` variable, but some java related tools look at `JAVA_HOME` on occasion. – Edwin Buck Mar 30 '11 at 22:30
  • @Edwin: Fixed the missing `\bin` as well. Thanks. – Ken White Mar 30 '11 at 22:40
  • 1
    With all due respect, Your statement is not accurate. Windows is applying the jre and jrelib variables at the 'HKLM' level which has a higher precedence than 'user variables' ... The installer will override the HKLM\SOFTWARE\javasoft\Java Runtime Environment setting adding entries for 'JavaHome' and 'RuntimeLib' ... At the command-line .. if these settings are not changed or manipulated they will be loaded into the environment before user settings are loaded ... quite annoying really. – Edward J Beckett Mar 25 '13 at 00:20
6

I had the same issue. I have a bunch of Java versions installed and for some reason Java 1.7 was being used instead of Java 1.6, even though I specified in the path to use 1.6 (C:\jdk1.6.0_45_32\bin).

I had to move the path of the JDK I wanted to use (1.6) to be the first entry in the PATH environment variable to make sure Windows uses 1.6 instead of 1.7.

So, for example, the PATH environment variable before was:

C:\Program Files (x86);...<other entries>;C:\dev\ant181\bin;C:\jdk1.6.0_45_32\bin

and after I moved the jdk to be first, it worked:

C:\jdk1.6.0_45_32\bin;C:\Program Files (x86);...<other entries>;C:\dev\ant181\bin

I guess the Windows installer of Java 1.7 installed it to some other directory already in the PATH, thus getting used first instead of the specified custom PATH entry C:\jdk1.6.0_45_32\bin;

eternalminerals.com
  • 395
  • 1
  • 4
  • 14
5

enter image description hereSuppose you have install JDK 10 after JDK 8 and in the system environment variable set path like "C:\ProgramData\Oracle\Java\javapath" then Java version control by this path. it will ignore JAVA_HOME even jdk 1.8 path set here So remove "C:\ProgramData\Oracle\Java\javapath" in path to get effect of JAVA_HOME path

Samir 007
  • 179
  • 2
  • 8
3

For my Case in 'Path' variable there was a parameter added like 'C:\ProgramData\Oracle\Java\javapath;'. This location was having java.exe, javaw.exe and javaws.exe from java 8 which is newly installed via jdk.exe from Oracle.

I've removed this text from Path where my Path already having %JAVA_HOME%\bin with it.

Now, the variable 'JAVA_HOME' is controlling my Java version which is I wanted.

this.girish
  • 1,296
  • 14
  • 17
2

I had Java 7 and 8 installed and I want to redirect to java 7 but the java version in my cmd prompt window shows Java 8.
Added Java 7 bin directory path (C:\Program Files\Java\jdk1.7.0_10\bin) to PATH variable at the end, but did not work out and shows Java 8. So I changed the Java 7 path to the starting of the path value and it worked.
Opened a new cmd prompt window and checked my java version and now it shows Java 7

Suresh
  • 1,491
  • 2
  • 22
  • 27
2

Set Path environment variable to your desired jdk/bin directory

David Levin
  • 344
  • 1
  • 8
1

In my case I had Java 7 and 8 (both x64) installed and I want to redirect to java 7 but everything is set to use Java 8. Java uses the PATH environment variable:

C:\ProgramData\Oracle\Java\javapath

as the first option to look for its folder runtime (is a hidden folder). This path contains 3 symlinks that can't be edited.

In my pc, the PATH environment variable looks like this:

C:\ProgramData\Oracle\Java\javapath;C:\Windows\System32;C:\Program Files\Java\jdk1.7.0_21\bin;

In my case, It should look like this:

C:\Windows\System32;C:\Program Files\Java\jdk1.7.0_21\bin;

I had to cut and paste the symlinks to somewhere else so java can't find them, and I can restore them later.

After setting the JAVA_HOME and JRE_HOME environment variables to the desired java folders' runtimes (in my case it is Java 7), the command java -version should show your desired java runtime. I remark there's no need to mess with the registry.

Tested on Win7 x64.

Broken_Window
  • 2,037
  • 3
  • 21
  • 47
1

This issue is probably because of the earlier versions of Java installed in your System. First check your Environment Variables Carefully and remove all the Environment Variables related to the previous versions of JAVA and replace those paths to

C:\Program Files\Java\<your new jdk version>\bin
scopchanov
  • 7,966
  • 10
  • 40
  • 68
Tanay Toshniwal
  • 142
  • 1
  • 11
0

There's an additional factor here; in addition to the java executables that the java installation puts wherever you ask it to put them, on windows, the java installer also puts copies of some of those executables in your windows system32 directory, so you will likely be using which every java executable was installed most recently.

Ben Brammer
  • 988
  • 4
  • 11
0

Just in case if you are using .BAT file as Windows Service, I would suggest to uninstall the Windows service and reinstall it again after changing the %JAVA_HOME% to point to the right Java version..

Aurun
  • 1
0

After struggling with this issue for some time and researching about it, I finally managed to solve it following these steps:

1) install jdk version 12
2) Create new variable in systems variable
3) Name it as JAVA_HOME and give jdk installation path
4) add this variable in path and move it to top.
5) go to C:\Program Files (86)\Common Files\Oracle\Java\javapath and replace java.exe and javaw.exe with the corresponding files with the same names from the pathtojavajdk/bin folder

Finally, I checked the default version of java in cmd with "java -version" and it worked!

0

I had the same problem, in user environment variable settings I was having JAVA_HOME and PATH configured properly but it didn't work. As I update my system environment variables then it started to work.

sunleo
  • 10,589
  • 35
  • 116
  • 196
0

I was facing the same issue. I had a java version of 8.11. I had these two in my path: C:\Program Files\Java\jdk1.8.0_51\bin and C:\ProgramData\Oracle\Java\javapath

What I did is that I had changed the jdk to %JAVA_HOME%\bin like this and renamed the directory (C:\ProgramData\Oracle\Java\javapath) of javapath to javapath1 and it resolved my problem.

Cadoiz
  • 1,446
  • 21
  • 31
Tejesh
  • 1
0

Delete C:\ProgramData\Oracle\Java\javapath from Path in system variables & add your custom value for JAVA_HOME variable for User variables. This will work in Windows 10.

JR Sahoo.'JS'
  • 1,338
  • 2
  • 10
  • 11
0

Folks who responded by stating setting PATH variable should work then please explain this:

enter image description here

You need to remove the entry C:\Program Files (x86)\Common Files\Oracle\Java\javapath from PATH and make sure you have %JAVA_HOME%\bin entry under PATH and JAVA_HOME defined.

Codistan
  • 1,469
  • 1
  • 13
  • 17
0

move your %JAVA_HOME%\bin to top of PATH in system environment variable

%JAVA_HOME%\bin;<rest of your PATH variables>