简体   繁体   中英

Is ProcessBuilder aware of environment variable?

https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html

Suppose that a java program (say, my.jar) uses ProcessBuilder to start subprocess.

If I start the program with VAR=xxx java -jar my.jar , will the subprocess be aware of the environment variable VAR is set to xxx?

Variables will be passed on if they are exported correctly - and that very much depends on your OS and command shell.

Before launching your Java app, try set VAR=xyz (Windows) or export VAR=xyz (GNU/Linux depending on your shell), and these should be passed onto java process and any sub-process called with ProcessBuilder - except where your code passes in environment() which may change the environment settings of the sub-process.

However on GNU/Linux with bash a local variable declaration such as NOTEXPORTED=XYZ isn't passed on to the java process, therefore isn't passed on sub-process. For the following example, VAR2 is passed to the java application but not VAR1 - unless changed to export VAR1=123 .

VAR1=123
VAR2=456 java -jar xyz.jar

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