Are you using ProcessBuilder for this? That allows simple access to the environment variables passed to sub-processes. It sounds like you are trying to modify the global environment, which is likely not what you want to do.
Here's an example:
ProcessBuilder pb = new ProcessBuilder();
Map<String, String> env = pb.environment();
System.out.println("Current environment: " + env.toString());
String path = env.get("PATH");
path = path.substring(0, path.indexOf("d:\\test")) + path.substring(path.indexOf("d:\\test") + "d:\\test".length());
env.put("PATH", path);
pb.command("ping");
Process p = pb.start();
// ...
Because it sounds like you are on Windows, you'll need to be a bit more careful about finding the path variable in the environment, since the Windows environment is case-insensitive, but Map.get()
is case-sensitive. Probably best to loop through the keys looking for a equalsIgnoreCase("PATH")
.
Also, you may want to clean up the path before putting it back into the map (make sure it doesn't contain extra System.getProperty("path.separator")
s.