LucienK
LucienK

Reputation: 310

Setting java path in .bat file

On Windows 8, I've got a bat file with the following contents:

@echo off
echo Setting JAVA_HOME
set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_39
echo setting PATH
set PATH=C:\Program Files\Java\jdk1.6.0_39\bin;%PATH%
echo Display java version
java -version

which I'm trying to use to switch java versions. When I run the .bat file, it outputs the version as 1.6.0_39, but doesn't actually switch versions. Instead, I've had to manually change the path variable every time I want to use a different version. Is there something wrong with the way that I'm setting the path? When I look at the path env variable after I've run this, it doesn't look like it's changed, and you'd think that it would be fairly visible...

Current version:

@echo off
echo Setting JAVA_HOME
setx JAVA_HOME "C:\Program Files\Java\jdk1.7.0_09"
echo Display java version
java -version

with %JAVA_HOME%\bin; at the front of my path variable. Doesn't seem to be looking at %JAVA_HOME%

Upvotes: 0

Views: 10106

Answers (2)

marsze
marsze

Reputation: 17154

SETX is often not supported. You can also change the registry setting. See >> this post

Upvotes: 0

SLaks
SLaks

Reputation: 888167

The SET statement sets an environment variable for the current process only.

Set the the global (default) value of an environment variable, use setx.

Note that even setx will not affect existing processes, so you will only notice the change in a new command prompt window.

Upvotes: 3

Related Questions