Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boolean.getBoolean() Vs System.getenv() in Java

Tags:

java

Boolean.getBoolean("myvariable"); // where myvariable has been defined in the
                                  // Environment variable as Variable name:
                                  // myvariable
                                  // and Variable Value:true

The above call gives me output as false. If I use

 System.getenv("myvariable") ; 

then it gives me output as true.

I am wondering why Boolean.getBoolean("myvariable") is not working.

like image 224
Prakash Avatar asked Jan 06 '12 14:01

Prakash


People also ask

What is difference between system Getenv and system getProperty?

getenv() is for Operating System environment variables, whereas System. getProperty() is for JVM arguments which are passed as -DpropName=value to Java application launcher ( java ).

What is system Getenv in Java?

getenv(String name) method gets the value of the specified environment variable. An environment variable is a system-dependent external named value. Environment variables should be used when a global effect is desired, or when an external system interface requires an environment variable (such as PATH).

Can system Getenv return null?

java - System. getenv() returns null when the environment variable exists - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.

Can environment variables be boolean?

Environment variables can never be a boolean, they are always a string (or not present).


2 Answers

System.getenv returns an environment variable. That's not the same thing as System.getProperty which returns a Java system property.

Boolean.getBoolean uses the latter call, as documented:

Returns true if and only if the system property named by the argument exists and is equal to the string "true". [...] A system property is accessible through getProperty, a method defined by the System class.

like image 144
Jon Skeet Avatar answered Sep 23 '22 10:09

Jon Skeet


Boolean.getBoolean("myvariable"); looks for a System Property called myvariable whereas System.getenv("myvariable"); looks for an environment variable. Though similar, they're not the same.

like image 21
Chris Avatar answered Sep 22 '22 10:09

Chris