简体   繁体   中英

Java how set environment variable in parent process

I have a bash script that at some point launches a java program. Inside the java program (for several reasons) I need to set an environment variable which should also be set in the parent bash process. How can I do this?

I've tried the following but it is not working:

String[] commands = new String[]{"command", "arg1", "arg2"};
ProcessBuilder processBuilder = new ProcessBuilder(commands);
processBuilder.environment().put("my_var", "my_value");
Process proc = processBuilder.start();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
proc.waitFor();

Child process cannot create new environment variable in the parent process from it's child. The flow of environment is downwards: Parent env -> Child env -> Child Child env etc...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM