Environment variables inwards Java
There are 2 ways to larn surroundings variable inwards Java, yesteryear using System properties or yesteryear using System.getEnv(). System properties provides exclusively express ready of predefined surroundings variables similar java.classpath, for retrieving Java Classpath or java.username to larn User Id which is used to run Java plan etc but a to a greater extent than robust together with platform independent agency of getting surroundings variable inwards Java program on the other mitt System.getEnv() method furnish access to all surroundings variables within Java plan but discipline to innovate platform dependency if plan relies on a item surroundings variable. System.getEnv() is overloaded method inwards Java API together with if invoked without parameter it returns an unmodifiable String map which contains all surroundings variables together with in that place values available to this Java procedure piece System.getEnv(String name) returns value of surroundings variable if exists or null. In our before posts nosotros convey seen How to larn electrical flow directory inwards Java together with How to run compaction ascendancy from Java program together with inwards this Java tutorial nosotros volition run into how to access surroundings variable inwards Java.
How to larn surroundings variables inwards Java - Example
String map of all surroundings variables piece System.getEnv(String name) only render value of named surroundings variable similar JAVA_HOME volition render PATH of your JDK installation directory.
/**
* Java plan to demonstrate How to larn value of surroundings variables inwards Java.
* Don't confuse betwixt System holding together with Environment variable together with in that place is separate
* agency to larn value of System holding than surroundings variable inwards Java, every bit shown inwards this
* example.
*
* @author Javin Paul
*/
public class EnvironmentVariableDemo {
public static void main(String args[]){
//getting username using System.getProperty inwards Java
String user = System.getProperty("user.name") ;
System.out.println("Username using organization property: " + user);
//getting username every bit surroundings variable inwards java, exclusively plant inwards windows
String userWindows = System.getenv("USERNAME");
System.out.println("Username using surroundings variable inwards windows : " + userWindows);
//name together with value of all surroundings variable inwards Java program
Map<String, String> env = System.getenv();
for (String envName : env.keySet()) {
System.out.format("%s=%s%n", envName, env.get(envName));
}
}
}
Output:
Username using organization property: harry
Username using surroundings variable inwards windows : harry
USERPROFILE=C:\Documents together with Settings\harry
JAVA_HOME=C:\Program Files\Java\jdk1.6.0_20\
TEMP=C:\DOCUME 1\harry\LOCALS 1\Temp
* Java plan to demonstrate How to larn value of surroundings variables inwards Java.
* Don't confuse betwixt System holding together with Environment variable together with in that place is separate
* agency to larn value of System holding than surroundings variable inwards Java, every bit shown inwards this
* example.
*
* @author Javin Paul
*/
public class EnvironmentVariableDemo {
public static void main(String args[]){
//getting username using System.getProperty inwards Java
String user = System.getProperty("user.name") ;
System.out.println("Username using organization property: " + user);
//getting username every bit surroundings variable inwards java, exclusively plant inwards windows
String userWindows = System.getenv("USERNAME");
System.out.println("Username using surroundings variable inwards windows : " + userWindows);
//name together with value of all surroundings variable inwards Java program
Map<String, String> env = System.getenv();
for (String envName : env.keySet()) {
System.out.format("%s=%s%n", envName, env.get(envName));
}
}
}
Output:
Username using organization property: harry
Username using surroundings variable inwards windows : harry
USERPROFILE=C:\Documents together with Settings\harry
JAVA_HOME=C:\Program Files\Java\jdk1.6.0_20\
TEMP=C:\DOCUME 1\harry\LOCALS 1\Temp
Getting surroundings variable inwards Java – Things to remember
Java is platform independent linguistic communication but in that place are many things which tin make a Java plan platform dependent e.g. using a native library. Since surroundings variables besides vary from i platform to to a greater extent than or less other e.g. from windows to Unix you lot postulate to endure fleck careful piece straight accessing surroundings variable within Java program. Here are few points which is worth noting :
1) Use organization properties if value of surroundings variable is available via system property e.g. Username which is available using "user.name" organization property. If you lot access it using surroundings variable straight you lot may postulate to inquire for unlike variable every bit it may endure unlike inwards Windows e.g. USERNAME together with Unix every bit USER.
2) Environment variables are case sensitive inwards Unix piece instance insensitive inwards Windows then relying on that tin over again brand your Java plan platform dependent.
3) System.getEnv() was deprecated inwards free JDK 1.3 inwards back upward of using System.getProperty() but reinstated over again inwards JDK 1.5.
That's all on how to larn surroundings variable inwards Java. Though you lot convey convenient method similar System.getEnv() which tin render value of surroundings variable, its improve to operate System.getProperty() to larn that value inwards a platform independent way, if that surroundings variable is available every bit organization holding inwards Java.
Further Learning
Complete Java Masterclass
How to convert Enum to String inwards Java