Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
getEnvironmentVariable()
is a static method of the SystemUtils
class that is used to retrieve the value of an environment variable.
If the variable cannot be read, then the method returns the default value passed as parameter.
SystemUtils
The definition of SystemUtils
can be found in the Apache Commons Lang
package, which we can add to the Maven project by adding the following dependency to the pom.xml
file:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
For other versions of the
commons-lang
package, refer to the Maven Repository.
You can import the SystemUtils
class as follows:
import org.apache.commons.lang3.SystemUtils;
public static String getEnvironmentVariable(String name, String defaultValue)
String name
: The environment variable name.String defaultValue
: The default value.This method returns the environment variable value. If there is no environment variable value, it returns the default value.
import org.apache.commons.lang3.SystemUtils;
public class Main{
public static void main(String[] args){
// Example 1
String envVarName = "HOME";
String defaultValue = "Variable not there";
System.out.printf("The output of SystemUtils.getEnvironmentVariable for '%s' is '%s'", envVarName, SystemUtils.getEnvironmentVariable(envVarName, defaultValue));
System.out.println();
// Example 2
envVarName = "random-env-name";
defaultValue = "Variable not there";
System.out.printf("The output of SystemUtils.getEnvironmentVariable for '%s' is '%s'", envVarName, SystemUtils.getEnvironmentVariable(envVarName, defaultValue));
System.out.println();
}
}
environment variable name = HOME
default value = "Variable not there"
The method returns /Users/educative
, as the environment variable HOME
is available.
environment variable name = random-env-name
default value = "Variable not there"
The method returns the default value, i.e., Variable not there
, as the environment variable is not there.
The output of the code is as follows:
The output of SystemUtils.getEnvironmentVariable for 'HOME' is '/Users/abhi'
The output of SystemUtils.getEnvironmentVariable for 'random-env-name' is 'Variable not there'
In our experience, we suggest you solve this What is SystemUtils.getEnvironmentVariable() in Java? and gain some new skills from Professionals completely free and we assure you will be worth it.
If you are stuck anywhere between any coding problem, just visit Queslers to get the What is SystemUtils.getEnvironmentVariable() in Java?
I hope this What is SystemUtils.getEnvironmentVariable() in Java? would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Coding Solutions >>