修改JAVA_HOME文件夹路径的部分内容,使用PowerShell

huangapple go评论62阅读模式
英文:

Modify part of the JAVA_HOME folder path using powershell

问题

我有一个应用程序,希望Windows的JAVA_HOME变量中不包含空格。<br/>
我想要一个高效的PowerShell脚本来获取当前文件夹路径,即<br/>
&quot;C:\program files\java\java.versionxxxx&quot;,仅将&quot;program files&quot;转换为&quot;progra~1&quot;,并相应地更新环境变量。

英文:

I have an application that wants the Windows JAVA_HOME variable not to contain spaces. <br/>
I would like an efficient PS script to get the current folder path i.e. <br/>
&quot;C:\program files\java\java.versionxxxx&quot;, convert only &quot;program files&quot; to &quot;progra~1&quot; and update the environment variable accordingly.

答案1

得分: 1

这应该能解决问题:

$newPath = $ENV:JAVA_HOME.replace('Program Files','Progra~1')

[System.Environment]::SetEnvironmentVariable("JAVA_HOME", "$NewPath", [System.EnvironmentVariableTarget]::Machine)

如果要修改系统环境变量,需要本地管理员访问权限。如果更改是针对特定用户配置文件的,则将 ::Machine 更改为 ::User

英文:

This should do the trick

$newPath = $ENV:JAVA_HOME.replace(&#39;Program Files&#39;,&#39;Progra~1&#39;)

[System.Environment]::SetEnvironmentVariable(&quot;JAVA_HOME&quot;, &quot;$NewPath&quot;, [System.EnvironmentVariableTarget]::Machine)

You need local administrator access to modify Machine environment variable. Change ::machine into ::user if the change is for a specific user profile

huangapple
  • 本文由 发表于 2020年9月11日 14:41:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63841989.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定