[英]Setting Path environment variable behavior differs on windows 10
I have an issue that is similar to what is found here but the behavior seems to be a bit different on Windows 10: Setting the environment for ProcessBuilder我有一个与此处发现的问题类似的问题,但在 Windows 10 上的行为似乎有些不同: 为 ProcessBuilder 设置环境
I want to set up the Path
environment variable in a ProcessBuilder
environment to send to a cmd /C
call.我想在ProcessBuilder
环境中设置Path
环境变量以发送到cmd /C
调用。
Consider:考虑:
if(platform.startsWith("Windows"))
{
cmd = "cmd";
command = new String[] {"/C", "prog.exe"};
}
String[] args = new String[]();
args.add(cmd);
args.add(command[0]);
args.add(command[1]);
ProcessBuilder pb = new ProcessBuilder(args);
Map<String, String> env = pb.environment();
// set environmental variables for libraries
if(platform.startsWith("Windows"))
{
env.put("Path", env.get("Path") + ";" + "C:\\test");
}
Process process = pb.start();
Using Path
is what is advised in the previous SO post, and that continues to work on Windows 7, but moving to Windows 10, it no longer finds prog.exe
.使用Path
是上prog.exe
SO 帖子中的建议,它继续在 Windows 7 上工作,但移动到 Windows 10,它不再找到prog.exe
。 What I dont understand is that if I change env.put("Path"...
to env.put("PATH"...
it now correctly finds prog.exe
. Has environment variables changed in Windows 10? Im also under the impression that Windows environment variables are case insensitive, but if I set both PATH
and Path
I see each distinctly listed in the environment for Windows 7 and 10.我不明白的是,如果我将env.put("Path"...
更改为env.put("PATH"...
现在可以正确找到prog.exe
。Windows 10 中的环境变量是否已更改?我也在Windows 环境变量不区分大小写的印象,但如果我同时设置PATH
和Path
我会在 Windows 7 和 10 的环境中看到每个都清楚地列出。
In your Java application, the keys of a Map are case-sensitive.在您的 Java 应用程序中,Map 的键区分大小写。 On the other hand, environment variable names are case-insensitive.另一方面,环境变量名称不区分大小写。 The Map returned by ProcessBuilder.environment()
will be able to keep separate entries keyed by "Path" and "PATH". ProcessBuilder.environment()
返回的 Map 将能够保留由“Path”和“PATH”键入的单独条目。 You can see the two separate entries when you list the contents of the Map.当您列出地图的内容时,您可以看到两个单独的条目。 However, when the Windows process is created, one of these two entries will overwrite the other in the process's environment table, since the name of the variable is deemed to be the same.但是,当创建 Windows 进程时,这两个条目之一将覆盖进程的环境表中的另一个,因为变量的名称被认为是相同的。 To see that it has created a single entry for path, you need to give the ProcessBuilder the command list {"cmd","/c","SET"} and look at the output of the process.要查看它是否为路径创建了单个条目,您需要为 ProcessBuilder 提供命令列表 {"cmd","/c","SET"} 并查看进程的输出。
I'm not sure why you say this behaviour differs between Windows 7 and 10. It shouldn't make any difference.我不确定你为什么说这种行为在 Windows 7 和 10 之间有所不同。它应该没有任何区别。
This problem seems to be covered by https://bugs.openjdk.java.net/browse/JDK-8245431 which talks about ProcessBuilder.environment()'s map being case-sensitive even when System.getenv() is case-insensitive. https://bugs.openjdk.java.net/browse/JDK-8245431似乎涵盖了这个问题,它谈到了 ProcessBuilder.environment() 的映射是区分大小写的,即使 System.getenv() 不区分大小写. The reporter expects the two to be consistent (as do I).记者希望两者保持一致(我也是如此)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.