# JAVA\_HOME on Windows

**URL:** <https://discuss.elastic.co/t/java-home-on-windows/34961>\
**Category:** Elasticsearch\
**Created:** [November 18, 2015, 3:26pm UTC](https://discuss.elastic.co/t/java-home-on-windows/34961 "2015-11-18T15:26:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrwensveen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mrwensveen/32/6006_2.png) [@mrwensveen](https://discuss.elastic.co/u/mrwensveen)\
**Post date:** [November 18, 2015, 3:26pm UTC](https://discuss.elastic.co/t/java-home-on-windows/34961/1 "2015-11-18T15:26:54Z")

</div>

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
```

---

<div class="post-metadata">

**Author:** ![SuperPringles](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/superpringles/32/5303_2.png) [@SuperPringles](https://discuss.elastic.co/u/SuperPringles)\
**Post date:** [November 18, 2015, 4:10pm UTC](https://discuss.elastic.co/t/java-home-on-windows/34961/2 "2015-11-18T16:10:03Z")

</div>

Interesting! Can I ask why you are refraining from installing JDK?

---

<div class="post-metadata">

**Author:** ![mrwensveen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mrwensveen/32/6006_2.png) [@mrwensveen](https://discuss.elastic.co/u/mrwensveen)\
**Post date:** [November 19, 2015, 12:54pm UTC](https://discuss.elastic.co/t/java-home-on-windows/34961/3 "2015-11-19T12:54:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![SuperPringles](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/superpringles/32/5303_2.png) [@SuperPringles](https://discuss.elastic.co/u/SuperPringles)\
**Post date:** [November 19, 2015, 12:56pm UTC](https://discuss.elastic.co/t/java-home-on-windows/34961/4 "2015-11-19T12:56:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![userf0fd722eb5](https://avatars.discourse-cdn.com/v4/letter/u/65b543/32.png) [@userf0fd722eb5](https://discuss.elastic.co/u/userf0fd722eb5)\
**Post date:** [January 25, 2017, 7:29pm UTC](https://discuss.elastic.co/t/java-home-on-windows/34961/5 "2017-01-25T19:29:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 10:03pm UTC](https://discuss.elastic.co/t/java-home-on-windows/34961/6 "2017-07-05T22:03:52Z")

</div>


