0

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();
revy
  • 3,945
  • 7
  • 40
  • 85
  • You cannot do that. And **exactly** what reasons? Sounds like XY-question. What problem are you ***actually*** trying to solve? – Elliott Frisch Mar 29 '18 at 14:00
  • possible dublicate: https://stackoverflow.com/questions/318239/how-do-i-set-environment-variables-from-java?rq=1 – arxakoulini Mar 29 '18 at 14:01

1 Answers1

0

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

CuriousGuy
  • 1,545
  • 3
  • 20
  • 42