I'm running Elasticsearch on a Windows machine with just a JRE (no JDK installed). Since the JDK installer usually creates the JAVA_HOME environment variable, I'm left without it. Since I don't want to set it manually every time the JRE decides to update (pretty often), I created a startup script that sets the JAVA_HOME variable based on the path of java.exe.
The requires the path of java.exe to be in the PATH variable. The Oracle JRE installer does this for you. I hope someone finds this useful.
@echo off :: Find java.exe symlink (C:\ProgramData\Oracle\Java\java.exe) for /f %%i in ('where java.exe') do set java_lnk=%%i :: Find symlink target for /f "tokens=2 delims=" %%h in ('dir %java_lnk%') do set java_dir=%%h :: Find parent directory for %%a in ("%java_dir%\..") do set "JAVA_HOME=%%~dpa" :: Remove trailing slash IF %JAVA_HOME:~-1%==\ SET parent=%JAVA_HOME:~0,-1% :: start ElasticSearch elasticsearch.bat