Skip to content

Instantly share code, notes, and snippets.

@darcyparker
Last active February 29, 2016 07:32
Show Gist options
  • Save darcyparker/7121448 to your computer and use it in GitHub Desktop.
Save darcyparker/7121448 to your computer and use it in GitHub Desktop.
Searches for latest JDK installed and sets %JAVA_HOME% to it. Useful for tools like ant which expect %JAVA_HOME% to be set to a JDK. This saves me time in having to reset %JAVA_HOME% after I update my JDK install.
@echo off
REM https://gist.github.com/darcyparker/7121448
REM Searches for latest JDK and sets JAVA_HOME
REM Look up current version
for /f "usebackq tokens=2*" %%i in (`reg query "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit" /s ^| "%WINDIR%\system32\find.exe" "CurrentVersion"`) do set _jdkcurrentver=%%j
REM Look up JavaHome for current version
for /f "usebackq tokens=2*" %%i in (`reg query "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Development Kit\%_jdkcurrentver%" /s ^| "%WINDIR%\system32\find.exe" "JavaHome"`) do set JAVA_HOME=%%j
echo JAVA_HOME=%JAVA_HOME%
set _jdkcurrentver=
setx JAVA_HOME "%JAVA_HOME%"
@darcyparker
Copy link
Author

See similar solution for unix: https://gist.github.com/darcyparker/7138415

@darcyparker
Copy link
Author

Note: I am using the full path to find.exe because there is a risk that the gnuwin32 or git bash version of find.exe comes first in the %PATH%. This batch file intends to use window's find.exe.

Also note: This batch file uses setx.exe which comes with windows 7. But not windows XP. For windows XP you will need to install Windows XP Service Pack 2 Support Tools

@levonk
Copy link

levonk commented Feb 29, 2016

Thanks for this, haven't tried it, but was going to ask

  • Does this still work with the change to Oracle
  • any easy way to update the system PATH and environments?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment