tags:

views:

49

answers:

4

Steps using runtime api

  1. echo %PATH% (output will be something like "c:\windows\system32;d:\test")
  2. execute ping or any system command, the output will be success
  3. delete one value for the path like d:\test
  4. echo %PATH% (output will be "%system32%\system32;")
  5. Now if I execute the same command executed in step 2 like ping, then I get "command not found."

Plesae note:- all steps executed in same java process

Anybody suggest whats going wrong in this process

A: 

It is because the path you are removing was used to resolve that command.

org.life.java
In my registry the value fo PATH is "%system32%\system32;d:\test" . But i dont know why there is difference between first echo and second echo command..
@user482914 what path you are removing ?
org.life.java
removing value d:\test from PATH key in registry
@user482914 why you are doing that ? and the last command that you are executing is located in d:\test i guess.
org.life.java
+1  A: 

Looks like variables are not being expanded the second time you show the PATH (step 4). Probably you are corrupting the path when you delete one value in step 3.

Show us a code snippet demonstrating the problem.

Grodriguez
A: 

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.

Jonathan
+1  A: 

Your question is not clear to me but i do see some problem :

%System32% is usually c:\windows\system32.

In your example,assuming the environment is set of %system32% correctly, the step(4) "%system32%\system32;" will resolve to c:\windows\system32\system32

Which may not what you want.

Ramp