Simiil
Simiil

Reputation: 2311

Changing the Environment Variables of a ProcessBuilder after the process started

I want to set Environment variables of a SubProcess, built via a ProcessBuilder, after it started. The following code does not work:

ProcessBuilder pb  = new ProcessBuilder("cscript.exe", "test.vbs");
Process p = pb.start();
pb.environment().put("test", "1");

Is there a way to do this?

Upvotes: 0

Views: 471

Answers (2)

Deepak Bala
Deepak Bala

Reputation: 11185

No you cannot do that. Process builder environments are isolated and not mutable after starting them.

Two ProcessBuilder instances always contain independent process environments, so changes to the returned map will never be reflected in any other ProcessBuilder instance or the values returned by System.getenv.

Subsequent modifications to this process builder will not affect the returned Process.

If you're interested in passing information to the process after starting it, use the OutputStream for the process and write to it. The process should read from its input stream and process the communication from the caller. More help.

OutputStream os = process.getOutputStream();  
// write data to this stream and read it on the other end.

Upvotes: 3

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 135992

It's impossible, see ProcessBuilder.start API: ... Subsequent modifications to this process builder will not affect the returned Process.

Upvotes: 2

Related Questions