Skip to content

Instantly share code, notes, and snippets.

@downgoon
Created July 10, 2017 11:09
Show Gist options
  • Save downgoon/cef51e475f5bfd36ba50a41cd52cfc70 to your computer and use it in GitHub Desktop.
Save downgoon/cef51e475f5bfd36ba50a41cd52cfc70 to your computer and use it in GitHub Desktop.
子进程的环境变量

子进程继承环境变量

读取环境变量

public class EnvEcho {

	public static void main(String[] args) {
		System.out.println("EnvEcho http_proxy: " + System.getenv("http_proxy"));
	}

}

启动子进程

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class EnvSub {

	public static void main(String[] args) throws Exception {
		System.out.println("main http_proxy: " + System.getenv("http_proxy"));
		
		Process sub = Runtime.getRuntime().exec("java EnvEcho");
		
		BufferedReader reader = new BufferedReader(new InputStreamReader(sub.getInputStream()));
		
		String line = "";
		while ((line = reader.readLine()) != null) {
			System.out.println("\tsub: "+ line);
		}
		
	}

}

实验

➜  tmp java EnvSub
main http_proxy: http://10.199.75.12:8080
	sub: EnvEcho http_proxy: http://10.199.75.12:8080
➜  tmp proxy -j
http_proxy: http://10.77.145.90:11328
https_proxy: http://10.77.145.90:11328
➜  tmp java EnvSub
main http_proxy: http://10.77.145.90:11328
	sub: EnvEcho http_proxy: http://10.77.145.90:11328
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment