windowsbatch-filejava-runtime-compiler

Getting the latest Java Runtime Enviroment parent folder


I want to make my job a little easier, I tried creating a batch file to get the JRE path. I'm only getting the javaw.exe path. So for example we are using Team Center 10. It has this tem_init and it grabs the folder JRE7 (example version) but people update to the latest version and uninstall. I'm looking to make a batch file that automatically grabs JRE installed.

    @echo off 
    setlocal enableextensions disabledelayedexpansion

    rem Where to find java information in registry
    set "javaKey=HKLM\SOFTWARE\JavaSoft\Java Runtime Environment"

    rem Get java home for current java version
    set "javaDir="
    for /f "tokens=2,*" %%d in ('reg query "%javaKey%\%javaVersion%" /v "JavaHome" 2^>nul') do set "javaDir=%%e"

    if not defined javaDir (
        echo Java directory not found
    ) else (
        set TC_JRE_HOME : %javaDir%
    )

:end 
   endlocal

I wanna get a path like this C:\Program Files\Java\jre7


Solution

  • You could try this, (it assumes that the latest version will be the last returned result):

    @Echo Off
    Set "baseKey=HKLM\SOFTWARE\JavaSoft\Java Runtime Environment"
    Set "regVal=JavaHome"
    Set "javaDir="
    For /F "Tokens=2*" %%A In (
        '"Reg Query "%baseKey%" /S /F "%regVal%" /V 2>Nul|Find /I "%regVal%""'
    ) Do Set "javaDir=%%~B"
    If Not Defined javaDir (Echo Java directory not found) Else (
        Set "TC_JRE_HOME=%javaDir%")
    Pause
    

    Edit

    That seems to Set a variable unnecessarily, this doesn't:

    @Echo Off
    Set "baseKey=HKLM\SOFTWARE\JavaSoft\Java Runtime Environment"
    Set "regVal=JavaHome"
    Set "TC_JRE_HOME="
    For /F "Tokens=2*" %%A In (
        '"Reg Query "%baseKey%" /S /F "%regVal%" /V 2>Nul|Find /I "%regVal%""'
    ) Do Set "TC_JRE_HOME=%%~B"
    If Not Defined TC_JRE_HOME Echo Java directory not found
    Pause
    

    I've added the Pause commands just to allow for reading any Echoes.