3

When using Boxstarter and or Chocolatey it is difficult to install JDK properly to use with Maven and other tools.

How can I add to environmental variables?

setx -m path "C:\java"

but this will clear previous environmental variable.

Suggesing this was a duplicate:I said I wanted "add" to the ev's not "set" and I was clearly looking for a cmd script

noobnoob
  • 115
  • 1
  • 2
  • 6

2 Answers2

2

This is my Powershell append test suggestion. Note the limitation of Windows previous to Windows 10.

Windows 8- WARNING: The data being saved is truncated to 1024 characters.

Powershell -Command " setx -m pathtest \"$env:path ";" $env:JAVA_HOME\""

user1198289
  • 637
  • 1
  • 5
  • 14
1

You should add new path to end of current path with this:

setx /M PATH "%PATH%;<your-new-path>"

So in your case it will be like this:

setx /M PATH "%PATH%;C:\java"
Mohsen
  • 4,536
  • 2
  • 27
  • 49
  • PS C:\WINDOWS\system32> setx /M PATHa "%PATHa%;C:\java" SUCCESS: Specified value was saved. PS C:\WINDOWS\system32> setx /M PATHa "%PATHa%;C:\java" SUCCESS: Specified value was saved. PS C:\WINDOWS\system32> setx /M PATHa "%PATHa%;C:\javal" SUCCESS: Specified value was saved. PS C:\WINDOWS\system32> setx /M PATHa "%PATHa%;C:\javall" ----------------------------------------------------------------------------------------------------------- creates %PATHa%;C:\javall what does that do? – user1198289 Dec 09 '18 at 22:23
  • 1
    @user1198289 so? – Mohsen Dec 09 '18 at 22:26
  • @user1198289 It's just `%PATH%`, not `%PATHa%` nor `javall` – OneCricketeer Dec 09 '18 at 22:30
  • 2
    Never use setx.exe to define `PATH`. This variable is defined in terms of other variables that get expanded at load time (e.g. `%UserProfile%`). More importantly, its definition is split into a per-machine value and a per-user value that get concatenated at load time. By updating it as `"%PATH%;"`, you make a horrible mess of both implementation details. – Eryk Sun Dec 10 '18 at 07:30
  • 1
    @user1198289 This is why I accepted your answer and your example is spot on. – noobnoob Dec 15 '18 at 20:11
  • @ErykSun, that is brilliant note which isn't obvious for not very experienced users like myself. That solution with `SETX` looks really nice and appealing, and I've almost run that `SETX` command happily. Good thing I came across your comment before. Looks like we have to do it directly via Windows registry. – 1234ru Mar 17 '21 at 20:04
  • @ErykSun, I found [decent article](http://www.dowdandassociates.com/blog/content/howto-set-an-environment-variable-in-windows-command-line-and-registry/) about getting and setting registry values from the command line, but they say there is no proper command-line way to make OS recognize new values. – 1234ru Mar 17 '21 at 20:17
  • This answer will delete everything you have in your system path and set it's new value to `"%PATH%;C:\java"` So please don't use this , if you must try it make sure that you have backed up your system path before trying. – Emmanuel David Dec 31 '22 at 02:44