Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Set JAVA_HOME on a Windows machine based on registry keys
@echo off
setlocal
:: Check if JAVA_HOME is already defined
IF DEFINED JAVA_HOME (exit /b 0)
:: Get the JDK version from the registry
set KEY="HKLM\SOFTWARE\JavaSoft\Java Development Kit"
set VALUE=CurrentVersion
reg query %KEY% /v %VALUE% 2>nul || (
echo JDK not installed
exit /b 1
)
set JDK_VERSION=
for /f "tokens=2,*" %%a in ('reg query %KEY% /v %VALUE% ^| findstr %VALUE%') do (
set JDK_VERSION=%%b
)
echo JDK VERSION: %JDK_VERSION%
:: Get the JDK path
set KEY="HKLM\SOFTWARE\JavaSoft\Java Development Kit\%JDK_VERSION%"
set VALUE=JavaHome
reg query %KEY% /v %VALUE% 2>nul || (
echo JavaHome not defined
exit /b 1
)
set JAVAHOME=
for /f "tokens=2,*" %%a in ('reg query %KEY% /v %VALUE% ^| findstr %VALUE%') do (
set JAVAHOME=%%b
)
setx -m JAVA_HOME "%JAVAHOME%"
endlocal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.