3

If I want to add the java \bin to my environment variable, can I do this from the command prompt using the SET command or is that just temporary?

1
  • 1
    It's nitpicking I know, but there's no DOS on NT versions of Windows. Dec 7, 2009 at 20:43

3 Answers 3

5

That's just temporary for the current process' environment. There's setx if you'd like to make a more permanent change.

5

Each process inherits the environment variables from the process that spawned it, and any changes they make using the SET command are local to that process (and any it spawns after the change) by default.

To make a change to an environment variable that “sticks”, whether for the current user or the whole system, you need to use a special tool. There are plenty of such tools, but I’ll give you a list of the best ones.

  • There is Microsoft’s SETX as mentioned by Ronald. It can be found in the resource kit or separately from a few Microsoft pages. It is generally a good one that most people will have no problems with. However, testing has found that it can fail to work, or even crash if the value is too long (it seems to have a limit of ~1KB). And as it happens, the PATH variable is the most likely variable to get really long…

  • There is a tool called SetEnv on CodeProject that performs this function. It is my current favorite because I have worked with the author to enhance it and work out some kinks; so it fulfills all of my expectations of such a tool.

  • There is another third party tool called SETENV which also performs this function.

  • Also, you could alter the variable through the MyComputer->Properties->Advanced->EnvironmentVariables interface.

  • Finally, you could go the manual way (I have written a script to do just that in a pinch), where you set the environment string directly to the registry and issue a WM_SETTINGCHANGE message to all top-level windows.

For the record, you asked about adding to the path. There is no default or built-in way to extend an environment variable (although such a function could easily be added to any of the above tools). Rather, when you change a variable, you replace it. Therefore, to add a path to the PATH variable, you would use a command such as:

SET PATH="%path%;C:\Newpath"

or

PATH "%path%;C:\Newpath"

But remember that neither of the previous commands will stick, so you can use the former command, replacing the call to SET with SETX, SetEnv, etc. The latter command cannot be made to stick.

HTH

1
  • SETX doesn't appear to use the VARNAME= syntax in win 7 at least. I had to set a path permanently with SETX PATH "%PATH%;C:\Newpath". Confused the crap out of me until I finally used the man-equivalent. Nov 8, 2014 at 18:27
0

From my experience it is temporary, and only effects the command window you issued it in. You have to modify the System settings via Control panel to make it happen for all new windows.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .