Hi.
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.
elasticsearch.custom.bat:
@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
1 Like
Interesting! Can I ask why you are refraining from installing JDK?
Hi SuperPringles,
We're mainly a Microsoft shop, doing C#/.Net development on IIS. I'm currently testing LogStash and ES on a development machine which doesn't have a JDK installed. I'd like to keep the machine as clean as possible, though installing a JDK probably wouldn't hurt.
We're likely to want a server JVM on production servers, though, so we'll probably install the JDK anyway.
If I remember correctly, installing a JDK on some Linux distributions doesn't automatically add a JAVA_HOME variable either, though it's probably easier to maintain with some symlinks.
Ahh so that's why. Yeah that's true. The JAVA_HOME isn't automatically added so that's another good way to use this script
This doesn't seem to work anymore, at least not as of Oracle JRE 1.8.0_121. The files in C:\ProgramData\Oracle\Java\javapath are not links; javapath itself is, but to another directory containing just the 3 java*.exe files.
I worked up a different batch file which is a little slower but seems to work for my use case:
@echo off
:: Locate what the system thinks is the active JRE base version
for /f "tokens=1,2*" %%i in ('reg.exe query "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment" /v CurrentVersion') do set java_version=%%k
:: Use the version value to find the key containing the JavaHome value
for /f "tokens=1,2*" %%i in ('reg.exe query "HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment%java_version%" /v JavaHome') do set JAVA_HOME=%%k
echo using JAVA_HOME=%JAVA_HOME%
"%~dp0elasticsearch.bat"
Hope this helps someone.