4

Once I install jdk 8 and jdk 10, jdk version 10 is getting configured as system JDK. Even though I change JAVA_HOME, PATH to jdk 8, system jdk does not changes. After changing path and JAVA_HOME if I check version of java in command prompt, it shows jdk 8. But if I double click an executable jar file it takes system jdk i.e. jdk 10.

I tried changing registry to edit the system jdk paths and versions but it didn't work.

From configure java I can only see jdk 10 as configured as system jdk and non editable.

Please help if you are able to switch jdk versions as system jdk.

Steps to replicate issue:

  1. install jdk 8
  2. install jdk 10
  3. without uninstalling jdk 10 change java home and path to jdk 8 from command prompt check java version, it will show jdk 8
    1. Now try to run an executable jar by double click on desktop. It will take jdk 10. That is the issue.

NOTE: This is not a duplicate post. There are posts on switching between jdk6 or 7 or 8 but not with jdk 10. JDK 10 also does not comes with zip distribution from oracle, it comes only as installable file.

DS_
  • 71
  • 1
  • 6
  • 2
    Which OS are you using? – Shubham Kadlag May 09 '18 at 09:06
  • 1
    See which version is set in your System PATH environment variable. The java command which is set in PATH is what will be picked up. – Naveen Kumar May 09 '18 at 09:06
  • 2
    JAVA_HOME is only used by some startup scripts to launch java software. It's not considered when when you type `java` into the command line (that's where `PATH` is used). When you click a jar on windows, I think it's not using the path, but registered file extensions (and maybe you can right-click and select "run with" to chose a different version). I'd just start the software via command line and put sth like `"C:\Program Files\Java\jdk1.8.0_131\bin\java.exe" -jar MyJar.jar` and adjust the path as needed – zapl May 09 '18 at 09:10
  • @nullpointer how did you arrive at the conclusion that this question was a duplicate? Which one of the quoted posts already answer the question "But if I double click an executable jar file it takes system jdk i.e. jdk 10"? – DodgyCodeException May 10 '18 at 10:49
  • I am using Windows 7. There is no java.exe inside system 32 folder. Path does not contains any duplicate entries. That's why from command prompt it takes jdk 1.8. – DS_ May 14 '18 at 06:27

4 Answers4

1

Usually, your PATH already contains a reference to %JAVA_HOME%\bin (e.g. on Windows), so adding / appending it again to the path will have no effect. You can either fix the PATH by removing the existing reference, or prepend the new setting.

On windows:

C:\>set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_1.41
C:\>set PATH=%JAVA_HOME%\bin;%PATH%

Now, the previous reference is still on the path, but after the new reference, thus it will be ignored.

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
1

There isn't officially any such thing as a "system jdk". When you double-click on a file, it's the file association that determines what program is used to open that file. How a file association is defined depends on your operating system. For example, on Windows, there are two places where file associations are stored. The first is in the Registry under HKLM\Classes, which is used by the command line, but this can be overridden by a Windows Explorer-specific association. It's probably not worth the hassle of changing file associations for .jar files every time you switch JDKs. Instead, just go to the command line and type java -jar myapp.jar. Then it will take whatever java version you've defined on the PATH.

DodgyCodeException
  • 5,963
  • 3
  • 21
  • 42
  • Actually there is, both Windows as well as Linux typically use a switching mechanism for the java (and others) executable in the default path. I think also MacOS does that. However I recommend to not use that (I.e. delete old `\windows\system32\java.exe‚ and remove newer `\programdata\oracle\java\javapath` from PATH on Windows) and add the correct bin directory from any java installation to the path first. – eckes May 13 '18 at 03:20
0

Depending on what platform you are on, there is usually a PATH environment that points to directories. Whatever is on there first, gets used. So on e.g. linux, it would pick up any java executables installed in /usr/bin and whatever jvm they point to.

You can bypass that by using absolute paths. Lots of command line software for java still relies on JAVA_HOME to figure out where the jvm is (e.g. gradle) so you can use that as well indeed.

Finally, on linux and osx, you can use e.g. jenv to control which jvm is used. You can also manage JAVA_HOME with that via a plugin (off by default).

This is what I have in my .bash_profile on my mac. Something similar should work on linux and windows as well.

# brew install jenv, install some jdks and make sure jenv knows where they are, and run this once to get jenv to export JAVA_HOME:
# jenv enable-plugin export
export PATH="$HOME/.jenv/bin:$PATH"
eval "$(jenv init -)"
Jilles van Gurp
  • 7,927
  • 4
  • 38
  • 46
0

I'm very late but I can solve this. You don't need to uninstall any of JDK. Install all JDK's in C:\Program Files\Java.

Create bat file from code & Run as Administrator. It will ask you to select JDK number, choose your JDK & hit enter. You just need to restart your app which is using JDK.

@echo off
echo "Current Java Version is"
echo %JAVA_HOME%
setlocal enableDelayedExpansion
SET PT="C:\Program Files\Java"
cd %PT%
::build "array" of folders
set folderCnt=0
for /f "eol=: delims=" %%F in ('dir /b /ad *') do (
  set /a folderCnt+=1
  set "folder!folderCnt!=%%F"
)

::print menu
for /l %%N in (1 1 %folderCnt%) do echo %%N - !folder%%N!
echo(

:get selection
set selection=
set /p "selection=Enter number to Set the JAVA_HOME and PATH "
echo you picked %selection% - !folder%selection%!
cd %PT%\!folder%selection%!
echo %cd%
( endlocal & rem return
   Set jdk=%cd%

)
echo Setting JAVA_HOME
SETX /M JAVA_HOME "%jdk%"
SET JAVA_HOME=%jdk%
echo JAVA_HOME %JAVA_HOME%
SET JAVA_BIN="%%JAVA_HOME%%\bin"
echo PATH %JAVA_BIN%
SETX PATH /M "%JAVA_BIN%;%PATH%" 
echo Java Version Changed
pause
Dipendra Sharma
  • 2,404
  • 2
  • 18
  • 35