How to check Java installation from script package?

I need to write a batch version of the script to find out if Java is installed, and if so, which way? I feel it should be something like this:

for /f %%j in ("java.exe") do (
   set JAVA_HOME=..........
)

but i can't figure it out.

PS It should work with a path with two spaces. For example, if java is set to "Program Files".

Thank.

+5
source share
4 answers

Using reg [.exe], you can query for possible JRE candidates that are installed on the system. They can not be, or there can be several.

In a test setup running inside a command shell:

reg query "HKLM\Software\JavaSoft\Java Runtime Environment"

I get three rows of results, the first of which CurrentVersion REG_SZ 1.6

Based on this, the request

reg query "HKLM\Software\JavaSoft\Java Runtime Environment\1.6\"

JavaHome REG_SZ C:\Program Files\Java\jre6

, , java .

32- Windows XP.

+7

"where"? :

>where java

?

:

C:\Users\myname>where java
C:\Program Files (x86)\Java\jdk1.6.0_17\bin\java.exe

C:\Users\myname>where foo
INFO: Could not find files for the given pattern(s).
+3

:

HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft

, , , reg.exe

+1

( )

::get javaw.exe from the latest properly installed jre
for /f tokens^=2^ delims^=^" %%i in ('reg query HKEY_CLASSES_ROOT\jarfile\shell\open\command /ve') do set JAVAW_PATH=%%i

::if reg entry is not found, java is not installed
if "%JAVAW_PATH%"=="" goto YOUR_ERROR

::then strip "\javaw.exe" from the JAVAW_PATH obtained above
set JAVA_HOME=%JAVAW_PATH:\javaw.exe=%

jre 1.6 1.7, windows xp , , .

+1

All Articles