I wish to set MAVEN_HOME in the “System Variables” using cmd -> run as administrator

好久不见. 提交于 2019-12-11 15:54:43

问题


I am trying to set the MAVEN_HOME environment variable using the setx command but in order to set it as a "System variable" and not the "User variable". In order to do this, I have to use -m with setx. Also, I wish to set MAVEN_HOME path as the current directory path in which the batch file is being run. I do this by

setx -m MAVEN_HOME "%cd%\apache-maven-3.6.1"

If I dont use -m then it sets the Env_variable as a user variable and if I use -m and run it as administrator then it does not pick the current directory path which is "C:\Users\nitin\Desktop\KristomInstall\apache-maven-3.6.1" using %cd%. Instead it picks

C:\WINDOWS\System32\ 

as the path using %cd%.

What I need is to use -m and run it as administrator along with a way to get the current location of the batch file I am running(as I was trying to do it using %cd%)

setx -m MAVEN_HOME "%cd%\apache-maven-3.6.1"

so I can distribute this batch file to other systems without manually adding the path to set MAVEN_HOME.


回答1:


Your question does not mention it, but the behavior suggests you're running this from a batch-file.

The first thing to note is that SetX with /m writes the variable to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment in the registry. That is a protected location which requires that your script is Run as administrator. When that happens the current directory is usually set to C:\WINDOWS\System32, as you have observed.

To cater for this, when your script uses relative paths, you should either change directory, at the outset, to the drive and path of the running script,

@Echo Off
CD /D "%~dp0"

SetX MAVEN_HOME "%CD%\apache-maven-3.6.1" /m

or replace the %CD% variable with the drive and path of the running script.

@Echo Off
SetX MAVEN_HOME "%~dp0apache-maven-3.6.1" /m

Please remember that the variable you have set will only take effect when a new command window is opened.



来源:https://stackoverflow.com/questions/57596107/i-wish-to-set-maven-home-in-the-system-variables-using-cmd-run-as-administr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!