7

I do most of my work against JDK 1.5 - but occasionally I have to change to 1.6. it is a bit painful to have to manually go and change my 'JAVA_HOME' system variable whenever I need to work on a project specific to one or the other (and no, Eclipse doesn't play well in these scenarios - trust me...I've tried.)

I'm looking for a registry script or windows shell script or for that matter any means by which I can "toggle" this system variable with something that is easy to run.

I've messed with the 'set' command, but that only sets the variable for that particular command instance - not globally.

Thanks in advance.

EDIT #1: Points of advise:

  • Use the JAVA_HOME variable in your path variable as well, that way you only have to change the JAVA_HOME (which is used in many projects anyways [maven, ant, etc])
  • Write the command into a couple batch scripts for easy use
  • When you make the change the windows command session will not reflect it right away. You must close and reopen it.
2
  • 3-d: You can use both "set" & "setx" in Your script, thus setting same environment variable for active user session & globally at the same time. Commented Dec 2, 2010 at 19:05
  • @barti - thanks for your help, for my scenario setx works perfectly. Thanks! Commented Dec 2, 2010 at 19:14

1 Answer 1

17

You could use setx for that purpose

Like so:

setx /M JAVA_HOME "C:\Program Files (x86)\Java\jdk1.6.0_17" 
Sign up to request clarification or add additional context in comments.

5 Comments

Yeah, I just noticed that command - do you have a semi-complete solution? (+1)
setx JAVA_HOME "c:\path" (HKCU) or setx JAVA_HOME "c:\path" -m (HKLM)
Cool, it was that '/M' that I was missing. Thanks!
Just wanted to add that as I put this in a couple of .bat files, I had to run them as administrator in order to make them work. Thanks it worked great!
Run cmd with administrative permissions. Also reopen applications that use %JAVA_HOME% or restart a computer to take effect.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.