由于测试环境在linux下,linux环境下变量的共享机制:
关于export
1.一个shell中的系统环境变量会被复制到子shell中(用export定义的变量);
2.一个shell中的系统环境变量只对该shell或者它的子shell有效,该shell结束时变量消失(并不能返回到父shell中)。
3.不用export定义的变量只对该shell有效,对子shell也是无效的。
而在java中,当程序中需要使用与操作系统相关的变量(例如:文件分隔符、换行符)时,Java提供了System类的静态方法getenv()和getProperty()用于返回系统相关的变量与属性,getenv方法返回的变量大多于系统相关,getProperty方法返回的变量大多与java程序有关。
系统属性和环境变量都是名称与值之间的映射。两种机制都能用来将用户定义的信息传递给 Java进程。环境变量产生更多的全局效应,因为它们不仅对Java子进程可见,而且对于定义它们的进程的所有子进程都是可见的。在不同的操作系统上,它们的语义有细微的差别,比如,不区分大小写。因此环境变量更可能有意料不到的副作用。程序中尽可能使用系统属性。环境变量应该在需要全局效应的时候使用,或者在外部系统接口要求使用环境变量时使用(比如 PATH)。
java启动子进程的方式:
public Process exec(String[] cmdarray, String[] envp) throws IOException
Executes the specified command and arguments in a separate process with the specified environment.
Given an array of strings cmdarray, representing the tokens of a command line, and an array of strings envp, representing "environment" variable settings, this method creates a new process in which to execute the specified command.
<strong>If envp is null, the subprocess inherits the environment settings of the current process.</strong>
ref:http://stackoverflow.com/questions/8997643/launch-a-child-process-from-java-that-doesnt-inherit-files-ports-on-unix