tags:

views:

1707

answers:

4

I'm switching between different Java SDKs (1.4.2, 1.5.0 and 1.6.0) for various projects. I would like to set the JAVA_HOME environment variable on my Windows XP machine without going through the tedious My Computer -> Advanced -> [Select System Variable] -> Edit -> Ok -> Ok

Is it possible to do this from the command line, or is there a utility that can do this?

(Note that I am not referring to the standard batch file "SET" command - I want to set the environment variable "globally," not just for the life of a console window).

+5  A: 

Hi,

from http://vlaurie.com/computers2/Articles/environment.htm:

Using the add-on tool Setx.exe

It is not part of the standard Windows XP setup but a command-line tool called setx.exe is included in the Windows XP Service Pack 2 Support Tools. This tool extends the set command so that permanent changes in the environment variables can be made. For example, to add a folder C:\New Folder to the path, the command would be setx path "%PATH%;C:\New Folder"

Regards, divo

0xA3
+1  A: 

Service Pack 2 Support Tools has a tool called "setx.exe" that can do what you are looking for. setx path "%PATH%;C:\New Folder"

Source

joegtp
+1  A: 

Here's some VBScript I use for this:

set args = WScript.Arguments
Set objShell = WScript.CreateObject("WScript.Shell")
Set colSystemEnvVars = objShell.Environment("System")
Set colUserEnvVars = objShell.Environment("User")

' Parse args
select case args.Count
case 0, 1, 2
    help
case 3
    sVariable = args(0)
    sValue = args(1)
    sScope = UCase(args(2))
    sMode = ""
case 4
    sVariable = args(0)
    sValue = args(1)
    sScope = UCase(args(2))
    sMode = UCase(args(3))
end select

select case sScope
    case "S"
        if sMode = "A" then
            sValue = colSystemEnvVars(sVariable) & sValue
        end if
        colSystemEnvVars(sVariable) = sValue
    case "U"
        if sMode = "A" then
            sValue = colUserEnvVars(sVariable) & sValue
        end if
        colUserEnvVars(sVariable) = sValue
    case else
        help
end select

WScript.Quit

'******************************************************************************
Sub help()
    WScript.Echo ""
    WScript.Echo "Create or update an environment variable."
    WScript.Echo ""
    WScript.Echo "usage:"
    WScript.Echo "======"  
    WScript.Echo "cscript SetVar.vbs variable value {S|U} [A]"
    WScript.Echo ""
    WScript.Echo "eg:"
    WScript.Echo "==="     
    WScript.Echo "cscript SetVar.vbs MYVAR 'Hello world' U"
    WScript.Echo "cscript SetVar.vbs PATH 'C:\MyPath' S A"

    WScript.Quit
End Sub

The scope can be 'S'ystem or 'U'ser. The last argument, 'A', if present, appends the value to the existing value of the variable (useful for adding a directoy to the PATH system variable).

The variables will presist, but you'll have to close then re-open a console to use them. I usually run this from the "Run..." dialog, then open a console.

Patrick Cuff
+1  A: 

Hasn't been said enough: I also strongly disagree with the statement that it's not programming related

Agreed - I'm back here looking at my own question because I'm dealing with yet another issue of a Java app that requires an environment variable to be set before running - for loading a JNI library!
noahz