Collect environment variable in java after running script through ProcessBuilder

穿精又带淫゛_ 提交于 2019-12-08 03:51:25

The ProcessBuilder.environment() method is used for passing the initial environment to the process when you call start(). You cannot get the environment of a subprocess from a parent process. This is not a Java restriction: you can't even get a subprocess environment from a Bash shell script (or in fact anything) that creates a subprocess. You need to find another means of communicating information from the subprocess back to the parent process.

In my opinion, you should change:

ProcessBuilder processBuilder = new ProcessBuilder("test.sh");

to

ProcessBuilder processBuilder = new ProcessBuilder("/bin/bash", "test.sh");
processBuilder.directory(new File(the-dir-of-test.sh));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!