0-0
0-0

Reputation: 482

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

I want to set up the Path environment variable in a ProcessBuilder environment to send to a cmd /C call.

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. 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.

Upvotes: 0

Views: 988

Answers (2)

Gili
Gili

Reputation: 90023

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. The reporter expects the two to be consistent (as do I).

Upvotes: 1

Klitos Kyriacou
Klitos Kyriacou

Reputation: 11621

In your Java application, the keys of a Map are case-sensitive. 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". 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. 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.

I'm not sure why you say this behaviour differs between Windows 7 and 10. It shouldn't make any difference.

Upvotes: 2

Related Questions