Iterating Through a Map of Environment Variables - Java Native OS

Java examples for Native OS:Environment

Description

Iterating Through a Map of Environment Variables

Demo Code

import java.util.Map; 

public class Main { 
    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"); 
        } /* w ww  .  ja v a2  s .com*/
        } else { 
            Map<String, String> vars = System.getenv(); 
            for(String var : vars.keySet()){ 
                System.out.println(var + " = " + vars.get(var)); 
            } 
        } 
    } 
}

Result


Related Tutorials