java父子进程之间继承了哪些信息?

13 篇文章 0 订阅

由于测试环境在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

     


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值