由於測試環境在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 IOExceptionExecutes 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