Get Environment varibales in java


import java.util.Map;

public class TestDemo {

public static void main(String[] args) {

String user = System.getProperty(“user.name”) ;  /*getting username using System.getProperty in Java */
System.out.println(“Username using system property: ”  + user);

String userWindows = System.getenv(“USERNAME”); /* getting username as environment variable in java, only works in windows */
System.out.println(“Username using environment variable in windows : ”  + userWindows);

//name and value of all environment variable in Java  program
Map<String, String> env = System.getenv();
for (String envName : env.keySet()) {
System.out.format(“%s=%s%n”, envName, env.get(envName));
}

}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s