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

[4496 byte] By [java80a] at [2007-11-27 11:06:13]
# 1

You are asking questions about Windows batch scripting in a Java forum?

When you get right down to it, the question is "How do I set an environment variable in a batch script", isn't it? Free your mind from useless distractions: the batch script and the operating system don't care at all what you're going to use that environment variable for.

Oh yeah, I almost forgot: you use the SET command to set an environment variable.

DrClapa at 2007-7-29 13:13:49 > top of Java-index,Java Essentials,Java Programming...
# 2

Thanks DrClap,

I tried the set i.e.:

set JAVA_HOME=%JavaHome%

echo %JAVA_HOME%

And it exho's the correct value but its not persistent. By that i mean, when catalina.bat tries to look for %JAVA_HOME%, its unable to find it.

anyways,I think I should seek help in the windows forums like you said.....

thanks

java80a at 2007-7-29 13:13:49 > top of Java-index,Java Essentials,Java Programming...