How to set JAVA_HOME programatically
Hi Friends,
Is there anyway to set JAVA_HOME environment property using a batch script.Actually,I am trying to check if the user system has java installed or not. If not,I make him run the JRE installer but the problem is when I try to start Tomcat, it expects JAVA_HOME to be set. So, I want to set JAVA_HOME programatically, is there anyway to do so?
This is the script i am using to check if java is already installed or not:
@echo off
::This batch file only tested under Windows 2000
::It will detect theshort path of the current version
::of the Java runtime executable
::First test to seeif we are on NT or similar OS by seeing
::if the ampersand is interpreted as a command separator
> reg1.txt echo 1234&rem
type reg1.txt | find"rem"
if not errorlevel 1goto WIN9X
::Find the current (most recent) Java version
start /w regedit /e reg1.txt"HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment"
type reg1.txt | find"CurrentVersion" > reg2.txt
if errorlevel 1goto ERROR
for /f"tokens=2 delims==" %%x in (reg2.txt)do set JavaTemp=%%~x
if errorlevel 1goto ERROR
echo Java Version = %JavaTemp%
del reg1.txt
del reg2.txt
::Get the home directory of the most recent Java
start /w regedit /e reg1.txt"HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment\%JavaTemp%"
type reg1.txt | find"JavaHome" > reg2.txt
if errorlevel 1goto ERROR
for /f"tokens=2 delims==" %%x in (reg2.txt)do set JavaTemp=%%~x
if errorlevel 1goto ERROR
echo Java home path (per registry) = %JavaTemp%
del reg1.txt
del reg2.txt
::Convertdouble backslashes to single backslashes
set JavaHome=
:WHILE
if"%JavaTemp%"==""goto WEND
if not"%JavaHome%"=="" set JavaHome=%JavaHome%\
for /f"delims=\" %%x in ("%JavaTemp%") do set JavaHome=%JavaHome%%%x
for /f"tokens=1,* delims=\" %%x in ("%JavaTemp%") do set JavaTemp=%%y
goto WHILE
:WEND
set JavaTemp=
echo Java home path (long, with spaces) = %JavaHome%
::Convertlong path (with spaces) into ashort path
for %%x in ("%JavaHome%")do set JavaHome=%%~dpsx
echo Java home path (short path, no spaces) = %JavaHome%
::Test the java path to seeif there really is a java.exe
if not exist %JavaHome%\bin\java.exegoto ERROR
::Make changes to the PATH
echo Insert code here that needs to know theshort path to Java.
set path=%JavaHome%\bin;%path%
goto DONE
:WIN9X
echo Insert code herefor Windows 9x
goto DONE
:ERROR
echo Insert code herefor conditions where Java.exe can't be found
goto DONE
:DONE
Thanks

