Java Reference
In-Depth Information
package org.java8recipes.chapter1.recipe1_11;
public class ReadOneEnvVariable {
public static void main(String[] args) {
if (args.length > 0) {
String value = System.getenv(args[0]);
if (value != null) {
System.out.println(args[0].toUpperCase()
+ " = " + value);
} else {
System.out.println("No such environment
variable exists");
}
} else {
System.out.println("No arguments passed");
}
}
}
If you are interested in retrieving the entire list of environment variables that is
defined on a system, do not pass any arguments to the System.getenv() method.
You'll receive back an object of type Map having all the values. You can iterate
through them as shown in Listing 1-10 .
Listing 1-10 . Iterating Through a Map of Environment Variables
package org.java8recipes.chapter1.recipe1_11;
import java.util.Map;
public class ReadAllEnvVariables {
public static void main(String[] args){
if(args.length > 0){
String value = System.getenv(args[0]);
if (value != null) {
System.out.println(args[0].toUpperCase() + "
= " + value);
} else {
 
 
Search WWH ::




Custom Search