52

I'm in the situation where I've installed the JDK, but I can't run applets in browsers (I may not have installed the JRE).

However, when I install the JRE, it clobbers my JDK as the default runtime. This breaks pretty much everything (Eclipse, Ant) - as they require a server JVM.

There's no JAVA_HOME environment variable these days - it just seems to use some registry magic (setting the system path is of no use either). Previously, I've just uninstalled the JRE after I've used it to restore the JDK. This time I want to fix it properly.

This also manifests itself with the jre autoupdater - once upon a time, I had a working setup with the JDK and JRE, but it updated and bust everything.

MarianD
  • 13,096
  • 12
  • 42
  • 54
Stephen
  • 19,488
  • 10
  • 62
  • 83
  • Curious; I've just got the "Famous Question" badge for this question, and it's only got 5 upvotes? Is this because: A) it's not a good question; B) people accidentally find it when looking for something else; C) This is really popular with the interwebs in general (non SO-users); or D) _(as always)_ "something else" – Stephen Feb 23 '12 at 01:10
  • [this worked for me where we can by default set the java version run time](https://stackoverflow.com/a/71671719/1557383) – vinny Mar 30 '22 at 04:59

9 Answers9

45

This is a bit of a pain on Windows. Here's what I do.

Install latest Sun JDK, e.g. 6u11, in path like c:\install\jdk\sun\6u11, then let the installer install public JRE in the default place (c:\program files\blah). This will setup your default JRE for the majority of things.

Install older JDKs as necessary, like 5u18 in c:\install\jdk\sun\5u18, but don't install the public JREs.

When in development, I have a little batch file that I use to setup a command prompt for each JDK version. Essentially just set JAVA_HOME=c:\jdk\sun\JDK_DESIRED and then set PATH=%JAVA_HOME%\bin;%PATH%. This will put the desired JDK first in the path and any secondary tools like Ant or Maven can use the JAVA_HOME variable.

The path is important because most public JRE installs put a linked executable at c:\WINDOWS\System32\java.exe, which usually overrides most other settings.

acdcjunior
  • 132,397
  • 37
  • 331
  • 304
nbeyer
  • 1,157
  • 10
  • 14
  • 15
    The key was to put the desired java/bin as the first thing in the system path. – Stephen Feb 15 '09 at 23:00
  • i had java_home set and new bin in path, didnt work, I had to install latest jre7 again and that took care of it – Kalpesh Soni Apr 24 '13 at 18:18
  • Very good and detailed answer, but missing the new best practice java introduces regarding path location switching. see [stackoverflow.com/questions/27996603](http://stackoverflow.com/questions/27996603/should-i-add-both-java-home-and-jre-home-in-environmental-variable-path/34733232#34733232). –  Jan 12 '16 at 00:58
  • @user257319 doesn't seems to me as "the new best practice java introduces", at least there is no reference, this is let say official practice... – Betlista Feb 08 '18 at 17:07
  • 1
    It's funny that you can't just set this in the Java Control Panel... you can set a lot of things there, half of it I don't even understand, but not this. – ddekany Mar 22 '18 at 09:38
7

I have patched the behaviour of my eclipse startup shortcut in the properties dialogue

from

"E:\Program Files\eclipse\eclipse.exe"

to

"E:\Program Files\eclipse\eclipse.exe" -vm "E:\Program Files\Java\jdk1.6.0_30\bin"

as described in the Eclipse documentation

It is a patch only, as it depends on the shortcut to fix things...

The alternative is to set the parameter permanently in the eclipse initialisation file.

Community
  • 1
  • 1
4

I just had that problem (Java 1.8 vs. Java 9 on Windows 7) and my findings are:

short version

default seems to be (because of Path entry)

c:\ProgramData\Oracle\Java\javapath\java -version

select the version you want (test, use tab completing in cmd, not sure what those numbers represent), I had 2 options, see longer version for details

c:\ProgramData\Oracle\Java\javapath_target_[tab]

remove junction/link and link to your version (the one ending with 181743567 in my case for Java 8)

rmdir javapath
mklink /D javapath javapath_target_181743567

longer version:

Reinstall Java 1.8 after Java 9 didn't work. The sequence of installations was jdk1.8.0_74, jdk-9.0.4 and attempt to make Java 8 default with jdk1.8.0_162...

After jdk1.8.0_162 installation I still have

java -version
java version "9.0.4"
Java(TM) SE Runtime Environment (build 9.0.4+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)

What I see in path is

Path=...;C:\ProgramData\Oracle\Java\javapath;...

So I checked what is that and I found it is a junction (link)

c:\ProgramData\Oracle\Java>dir
 Volume in drive C is OSDisk
 Volume Serial Number is DA2F-C2CC

 Directory of c:\ProgramData\Oracle\Java

2018-02-07  17:06    <DIR>          .
2018-02-07  17:06    <DIR>          ..
2018-02-08  17:08    <DIR>          .oracle_jre_usage
2017-08-22  11:04    <DIR>          installcache
2018-02-08  17:08    <DIR>          installcache_x64
2018-02-07  17:06    <JUNCTION>     javapath [C:\ProgramData\Oracle\Java\javapath_target_185258831]
2018-02-07  17:06    <DIR>          javapath_target_181743567
2018-02-07  17:06    <DIR>          javapath_target_185258831

Those hashes doesn't ring a bell, but when I checked

c:\ProgramData\Oracle\Java\javapath_target_181743567>.\java -version
java version "1.8.0_162"
Java(TM) SE Runtime Environment (build 1.8.0_162-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)

c:\ProgramData\Oracle\Java\javapath_target_185258831>.\java -version
java version "9.0.4"
Java(TM) SE Runtime Environment (build 9.0.4+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)

so to make Java 8 default again I had to delete the link as described here

rmdir javapath

and recreate with Java I wanted

mklink /D javapath javapath_target_181743567

tested:

c:\ProgramData\Oracle\Java>java -version
java version "1.8.0_162"
Java(TM) SE Runtime Environment (build 1.8.0_162-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)

** update (Java 10) **

With Java 10 it is similar, only javapath is in c:\Program Files (x86)\Common Files\Oracle\Java\ which is strange as I installed 64-bit IMHO

.\java -version
java version "10.0.2" 2018-07-17
Java(TM) SE Runtime Environment 18.3 (build 10.0.2+13)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.2+13, mixed mode)
Betlista
  • 10,327
  • 13
  • 69
  • 110
1

After many attempts, I found the junction approach more convenient. This is very similar on how this problem is solved in linux.

Basically it consists of having a link between c:\tools\java\default and the actual version of java you want to use as default in your system.


How to set it:

  1. Download junction and make sure to put it in your PATH environment variable
  2. Set your environment this way: - PATH pointing to ONLY to this jre c:\tools\java\default\bin - JAVA_HOME pointing to `c:\tools\java\default
  3. Store all your jre-s in one folder like (if you do that in your Program FIles folder you may encounter some
    • C:\tools\Java\JRE_1.6
    • C:\tools\Java\JRE_1.7
    • C:\tools\Java\JRE_1.8
  4. Open a command prompt and cd to C:\tools\Java\
  5. Execute junction default JRE_1.6

This will create a junction (which is more or less like a symbolic link in linux) between C:\tools\java\default and C:\tools\java\JRE_1.6

In this way you will always have your default java in c:\tools\java\default.

If you then need to change your default java to the 1.8 version you just need to execute

junction -d default
junction default JRE_1.8 

Then you can have batch files to do that without command prompt like set_jdk8.bat set_jdk7.bat

As suggested from @СӏаџԁеМаятіи

EDIT: From windows vista, you can use mklink /J default JRE_1.8

snovelli
  • 5,804
  • 2
  • 37
  • 50
  • 2
    That's what I do. But now it's `mklink /J` instead of `junction` for newer version of windows. You can simply use `rmdir` to safely delete the junction (it won't affect the linked folder). – Claude Martin Mar 24 '16 at 09:16
1

I simply install all the versions of JDK I need and the latest installed becomes default, so I just reinstall the one I want to be default if necessary.

John Mikic
  • 640
  • 6
  • 11
  • That's sad I have to reinstall let say Java 1.8 when I installed Java 9 for testing... As I found later even reinstall did not work for me, check [my answer](https://stackoverflow.com/a/48690194/384674) – Betlista Feb 08 '18 at 16:34
1

I have several JDK (1.4, 1.5, 1.6) installed in C:\Java with their JREs. Then I let Sun update the public JRE in C:\Program Files\Java.
Lately there is an improvement, installing in jre6. Previously, there was a different folder per new version (1.5.0_4, 1.5.0_5, etc.), which was taking lot of space

MarianD
  • 13,096
  • 12
  • 42
  • 54
PhiLho
  • 40,535
  • 6
  • 96
  • 134
  • You might add detail on how to "let Sun update the public JRE ...." Your reply is not very helpful as it is. – John Tobler Jan 25 '12 at 19:56
  • 1
    When you install a JDK on Windows, using their .exe program, in the middle of the installation it asks if it can install the JRE. That's what I meant by "let Sun [now Oracle...] update the public JRE". I always change the default location of the JDK (to a path without spaces, where I put all my Java programs) but always keep the default JRE installation path. – PhiLho Jan 27 '12 at 15:00
  • Your additional explanation is helpful and adds value, @PhiLho. Thank you! – John Tobler Feb 01 '12 at 22:38
0

an alterable way to run an .jar app is create an .bat cmd for it. for example, you have jre10 and jre8 installed on your pc,and jre10 is your default jre. but your jar is specified to work with jre8,following cmd will work:

"C:\Program Files\Java\jre1.8.0_181\bin\java.exe" -jar JabRef-4.3.1.jar
ndtc
  • 420
  • 5
  • 4
0

Need to remove C:\Program Files (x86)\Common Files\Oracle\Java\javapath from environment and replace by JAVA_HOME which is works fine for me

Subedi
  • 1
0

Stacked by this issue and have resolved it in 2020, in Windows 10. I'm using Java 8 RE and 14.1 JDK and it worked well until Eclipse upgrade to version 2020-09. After that I can't run Eclipse because it needed to use Java 11 or newer and it found only 8 version. It was because of order of environment variables of "Path":

environment variables setting

I suppose C:\Program Files (x86)\Common Files\Oracle\Java\javapath is path to link to installed JRE exe files (in my case Java 8) and the issue was resolved by move down this link after %JAVA_HOME%, what leads to Java 14.1/bin folder.

EV setting after edit

It seems that order of environment variables affects order of searched folders while executable file is requested. Thanks for your comment or better explanation.

pringi
  • 3,987
  • 5
  • 35
  • 45