0

When installing the latest JRE 7 on Windows, it no longer adds the command java to the system path. So just calling java --version in bat file fails in that case (despite that Java from java.com is installed).

What's a reliable way to find the java command installation directory in a windows bat file?

I've seen it in the following locations:

  • C:\Program Files\Java\jre6\bin\java.exe
  • C:\Program Files\Java\jre7\bin\java.exe
  • C:\Program Files (x86)\Java\jre7\bin\java.exe
  • Not tried JRE 8 yet.

Note: if there are multiple, I 'd like the default (or the latest - I don't care).

Geoffrey De Smet
  • 22,431
  • 8
  • 59
  • 106
  • 1
    See here, there is a batch script: http://stackoverflow.com/questions/3930383/jre-installation-directory-in-windows – sina72 Jun 30 '14 at 09:33

2 Answers2

3

If it is installed, ask windows where it is

@echo off 
    setlocal enableextensions disabledelayedexpansion

    rem Where to find java information in registry
    set "javaKey=HKLM\SOFTWARE\JavaSoft\Java Runtime Environment"

    rem Get current java version
    set "javaVersion="
    for /f "tokens=3" %%v in ('reg query "%javaKey%" /v "CurrentVersion" 2^>nul') do set "javaVersion=%%v"

    rem Test if a java version has been found
    if not defined javaVersion (
        echo Java version not found
        goto endProcess
    )

    rem Get java home for current java version
    set "javaDir="
    for /f "tokens=2,*" %%d in ('reg query "%javaKey%\%javaVersion%" /v "JavaHome" 2^>nul') do set "javaDir=%%e"

    if not defined javaDir (
        echo Java directory not found
    ) else (
        echo JAVA_HOME : %javaDir%
    )

:endProcess 
    endlocal
MC ND
  • 65,671
  • 6
  • 67
  • 106
  • What does `setlocal enableextensions disabledelayedexpansion` do? Is it needed? – Geoffrey De Smet Jun 30 '14 at 10:04
  • 1
    @GeoffreyDeSmet, `setlocal` protects the environment variables from changes made inside the `setlocal/endlocal`, `enableextensions` is usually not needed as extensions are enabled by default, but it is just a good habit to ensure you will have not problems. `disabledelayedexpansion` is included just to prevent some parser errors when assigning values to variables when the values include exclamations marks (that will be parsed/interpreted/removed) – MC ND Jun 30 '14 at 10:09
  • I tried this on 1 Windows machine (win 8) with the latest java of java.com (1.7), and the registry key was not `HKLM\SOFTWARE\JavaSoft\Java Runtime Environment`, but `HKLM\SOFTWARE\Wow6432Node\JavaSoft\Java Runtime Environment` instead. **Is there any way to deal with any and all of these deviations?** – Geoffrey De Smet Jul 02 '14 at 10:38
  • Apparently Wow6432Node is the only common deviation, so an extra check for that regkey solves it. Thanks :) – Geoffrey De Smet Jul 02 '14 at 13:47
  • [Here is the bat script that we're currently successfully using](https://github.com/droolsjbpm/optaplanner/blob/master/optaplanner-distribution/src/main/assembly/scripts/runExamples.bat) (which is based on this answer). – Geoffrey De Smet Jan 27 '16 at 09:25
1

This is how to find them.

Dir c:\java.exe /a/s 

searches entire drive.

You can put it in a for loop. (in for loops %A becomes %%A ina batch)

@for /f "tokens=1-8 delims=/ " %A in ('dir "c:\program files\java.exe" /a /s^|findstr /i /v /r "DIR VOL \(s\) Listed"') do echo day %A month %B Year %C Name %G  

This finds it and puts day month year into seperate variables.

For /?
dir /?
findstr /?
Noodles
  • 1,931
  • 1
  • 8
  • 4