2012-01-06 29 views
5
Boolean.getBoolean("myvariable"); // where myvariable has been defined in the 
            // Environment variable as Variable name: 
            // myvariable 
            // and Variable Value:true 

上述调用使我输出为false。 如果我使用Java中的Boolean.getBoolean()与System.getenv()

System.getenv("myvariable") ; 

然后它给了我输出true

我很奇怪,为什么Boolean.getBoolean("myvariable")不工作。

+0

我猜你的评论'testbootlog'是什么是正常传递给'getBoolean'? – 2012-01-06 14:30:18

+2

如果方法具有类似于'Boolean.getSystemPropertyAsBoolean'一些明智的名称,将清除校正所有混乱:) – 2012-01-06 14:51:06

+0

是@JamesMontagne,谢谢。 – Prakash 2012-01-06 20:40:32

回答

11

System.getenv返回环境变量。这与System.getProperty不一样,它返回Java系统属性

Boolean.getBoolean使用后者调用,如记载:

返回true当且仅当存在由参数命名的系统属性和等于字符串“true”。 [...]系统属性可通过getProperty访问,System类定义了一个方法。

+0

感谢@乔恩飞碟双向,我想都得到了环境变量:) – Prakash 2012-01-06 20:42:53

3

Boolean.getBoolean("myvariable");查找名为myvariable的系统属性,而System.getenv("myvariable");查找环境变量。虽然相似,但它们并不相同。

+0

环境variableA的和系统属性只是“类似”,因为它们是一组名称 - 值对。他们来自完全不同的来源。 – 2012-01-06 14:35:05

+0

谢谢Chris,@Stephen C,这很有帮助。 – Prakash 2012-01-06 20:44:23

相关问题