-1

I am trying get the latest JRE installed in windows using dir command. I Have JRE 1.6 and 1.8 installed on my Windows but I need to get whatever the latest version installed in windows (even in future it may change to 1.8 to 2.0*). Can any one please help on this.

Mounika
  • 23
  • 1
  • 5
  • 2
    Possible duplicate of [Which JRE I am using](http://stackoverflow.com/questions/8472121/which-jre-i-am-using)...please spend some time searching SO before posting a question. – Tim Biegeleisen Oct 21 '16 at 06:09
  • `set JAVA_HOME=[PATH TO DESIRED JRE]` – Timothy Truckle Oct 21 '16 at 06:24
  • In a nutshell: you can't I have 6 different JDKs and 4 different JREs on my system. Only 1 of the JDKs is visible in the registry - and that's not the latest. The only reliable way would be to scan the whole harddisk for the `java.exe` (or `javaw.exe`) call each one using `java -version` and parse the output. –  Oct 21 '16 at 06:26

2 Answers2

1

Try this batch for win7

@echo off



@echo off
rem http://technet.microsoft.com/en-us/library/bb490890.aspx
SETLOCAL EnableDelayedExpansion
:: findJDK.bat

rem start:-> Run a command in a separate process, or run a file with its default associated application
rem REGEDIT.EXE: -> Export to a (.REG) file: 
rem REGEDIT.EXE [ /L:system | /R:user ] /E exportfile.REG "registry_key"

start /w regedit /e reg1.txt "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\JavaSoft\Java Development Kit" 

rem TYPE: -> Display text file content in console
rem FIND: -> Search files or standard output

rem reserved key words regarding reg-key -> "JavaHome" "MicroVersion" "RuntimeLib"
rem | Reads the output from one command and writes it to the input of another command. Also known as a pipe. 

type reg1.txt | find "JavaHome" > reg2.txt 

if errorlevel 1 goto ERROR 
for /f "tokens=2 delims==" %%x in (reg2.txt) do (
  set JavaTemp=%%~x
  echo Regedit: JAVA_HOME path : !JavaTemp!
)

if errorlevel 1 goto ERROR
echo.
set JAVA_HOME=%JavaTemp%
set JAVA_HOME=%JAVA_HOME:\\=\%
echo JAVA_HOME was found to be %JAVA_HOME%



start /w regedit /e reg3.txt "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\JavaSoft\Java Runtime Environment" 
type reg3.txt | find "RuntimeLib" > reg4.txt 
if errorlevel 1 goto ERROR 
for /f "tokens=2 delims==" %%x in (reg4.txt) do (
  set JavaTemp=%%~x
  echo Regedit: JRE_HOME path : !JavaTemp!
)
if errorlevel 1 goto ERROR
echo.
set JRE_HOME=%JavaTemp%
set JRE_HOME=%JRE_HOME:\\=\%
echo JRE_HOME was found to be %JRE_HOME%
goto END
:ERROR
echo A reg1.txt is: & type reg1.txt
echo B reg2.txt is: & type reg2.txt
echo
:END
echo END

pause
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
0
java -version:1.8.0_101 -jar jarname.jar 

Use above way to invoke java here you can give version of java which you want to use in case there are multiple in your path .

gladiator
  • 1,249
  • 8
  • 23
  • Interesting, I didn't know this. But this only seems to work for JREs not for JDKs. My JDK 1.8 installation is not detected using `java -version:1.8 ...` even though `HKLM\SOFTWARE\JavaSoft\Java Development Kit\CurrentVersion` points to the correct installation –  Oct 21 '16 at 06:31