How to determine location of 32BIT Java

大兔子大兔子 提交于 2019-12-07 20:30:26
Frank

As I found out the 64 Bit Java Runtime Installation does not override the registry keys of the 32 bit Version. If you use the 32 bit Version of regedit.exe you will find the registry keys which belong to the 32 bit Java Runtime Environment Installation.

So the following Batch file should solve the above issue:

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@ECHO OFF
:: Execution on a 64 BIT system?
if defined PROGRAMFILES(x86) (set "REGEDITLOC=%windir%\SysWOW64\") else set "REGEDITLOC="

:: Define local parameters
set "TEMP_FILE_JAVA_REGISTRY_CONTENT=%~dp0java_runtime_environment.reg"

:: Export java Runtime Keys from registry to a temporary file
START /W %REGEDITLOC%REGEDIT /E "%TEMP_FILE_JAVA_REGISTRY_CONTENT%" "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment"

:: Determine the install directory of java
FOR /F "tokens=1* delims==" %%A IN ('TYPE "%TEMP_FILE_JAVA_REGISTRY_CONTENT%" ^| FIND "JavaHome"') DO SET JAVA_RUNTIME_HOME=%%B

:: Remove not required chars
SET JAVA_RUNTIME_HOME=%JAVA_RUNTIME_HOME:"=%
SET JAVA_RUNTIME_HOME=%JAVA_RUNTIME_HOME:\\=\%
@echo %JAVA_RUNTIME_HOME%

:: Delete temp file
@del "%TEMP_FILE_JAVA_REGISTRY_CONTENT%" /S /Q > NUL 2>&1
@ECHO ON
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

contribution from rojo:

The above script illustrates the correct idea, but exporting a .reg file and scraping it can be a bit cumbersome. Here's the same idea, but using %windir%\syswow64\reg.exe query to query the registry values in-line:

@ECHO OFF
setlocal

if defined PROGRAMFILES(x86) (set "reg=%windir%\SysWOW64\reg") else set "reg=reg"
set "branch=HKLM\Software\JavaSoft\Java Runtime Environment"

for /f "tokens=3" %%v in ('%reg% query "%branch%" /v "CurrentVersion" ^| find "REG_SZ"') do (
    for /f "tokens=2*" %%I in ('%reg% query "%branch%\%%v" /v "JavaHome" ^| find "REG_SZ"') do (
        set "$JAVA=%%J"
    )
)

echo Location of java.exe: %$JAVA%\bin
rojo

Edit: Frank's solution is the correct one. See my addition to his answer.


Previous answer: How about scraping the location from the start menu shortcut? The shortcut should always be in the All Users Start Menu → Programs unless a user manually deletes it, which in nearly two decades of desktop support I can't say I've ever seen happen. The shortcut should always point to the default instance of Java, even if there are multiple versions installed. And the installer has no option for the end user to forego creation of the start menu shortcuts.

Anyway, wmic path win32_shortcutfile can read the target of a shortcut.

As commented below, the Start Menu shortcuts could point to the 64-bit install of Java. If that's the case, then fall back to searching %PROGRAMFILES(x86)% for java.exe.

@echo off
setlocal

set "lnk=%PROGRAMDATA%\Microsoft\Windows\Start Menu\Programs\Java\About Java.lnk"

if defined PROGRAMFILES(x86) (set "PF=%PROGRAMFILES(x86)%") else set "PF=%PROGRAMFILES%"

for /f "tokens=1* delims==" %%I in (
    'wmic path win32_shortcutfile where "name='%lnk:\=\\%'" get target /format:list 2^>NUL ^| find "="'
) do set "$JAVA=%%~dpJ"

if not defined $JAVA goto notfound

2>&1 "%$JAVA%\java.exe" -version | find /i "64-bit" >NUL && (
    (
        for /f "delims=" %%I in ('dir /s /b /o:n "%PF%\*java.exe" 2^>NUL') do set "$JAVA=%%~dpI"
    ) || (
        goto notfound
    )
)

echo Latest 32-bit Java lives in %$JAVA%
goto :EOF

:notfound
echo Please install 32-bit Java.
goto :EOF
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!