Wensveen's Blog

November 18, 2015

Finding JAVA_HOME on Windows

Filed under: Uncategorized — wensveen @ 3:46 pm

I’m running a program on a Windows machine that needs JAVA_HOME to be set. Because I’m running it with just a JRE (no JDK installed) the JAVA_HOME environment variable isn’t set (the JDK installer usually creates the JAVA_HOME environment variable). Because I don’t want to modify my scripts or my environment manually every time the JRE decides to update (pretty often), I created a script that sets the JAVA_HOME variable based on the path of java.exe.

The batch script requires the path of java.exe to be in the PATH. The Oracle JRE installer usually does this for you.

set_java_home.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%
Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: