How To Buy The Farm Surroundings Variables Inwards Java- Illustration Tutorial
Environment variables inwards Java
There are 2 ways to larn surround variable inwards Java, past times using System properties or past times using System.getEnv(). System properties provides exclusively express laid upward of predefined surround 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 in addition to platform independent means of getting surround variable inwards Java program on the other manus System.getEnv() method render access to all surround variables within Java plan but dependent area to innovate platform dependency if plan relies on a detail surround variable. System.getEnv() is overloaded method inwards Java API in addition to if invoked without parameter it returns an unmodifiable String map which contains all surround variables in addition to at that topographic point values available to this Java procedure acre System.getEnv(String name) returns value of surround variable if exists or null. In our before posts nosotros accept seen How to larn electrical current directory inwards Java in addition to How to run trounce ascendancy from Java program in addition to inwards this Java tutorial nosotros volition run into how to access surround variable inwards Java.
How to larn surround variables inwards Java - Example
String map of all surround variables acre System.getEnv(String name) only render value of named surround variable similar JAVA_HOME volition render PATH of your JDK installation directory.
/**
* Java plan to demonstrate How to larn value of surround variables inwards Java.
* Don't confuse betwixt System holding in addition to Environment variable in addition to at that topographic point is separate
* means to larn value of System holding than surround variable inwards Java, equally 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 equally surround variable inwards java, exclusively plant inwards windows
String userWindows = System.getenv("USERNAME");
System.out.println("Username using surround variable inwards windows : " + userWindows);
//name in addition to value of all surround 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 surround variable inwards windows : harry
USERPROFILE=C:\Documents in addition to 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 surround variables inwards Java.
* Don't confuse betwixt System holding in addition to Environment variable in addition to at that topographic point is separate
* means to larn value of System holding than surround variable inwards Java, equally 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 equally surround variable inwards java, exclusively plant inwards windows
String userWindows = System.getenv("USERNAME");
System.out.println("Username using surround variable inwards windows : " + userWindows);
//name in addition to value of all surround 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 surround variable inwards windows : harry
USERPROFILE=C:\Documents in addition to Settings\harry
JAVA_HOME=C:\Program Files\Java\jdk1.6.0_20\
TEMP=C:\DOCUME 1\harry\LOCALS 1\Temp
Getting surround variable inwards Java – Things to remember
Java is platform independent linguistic communication but at that topographic point are many things which tin forcefulness out make a Java plan platform dependent e.g. using a native library. Since surround variables equally good vary from ane platform to to a greater extent than or less other e.g. from windows to Unix you lot postulate to live on fleck careful acre straight accessing surround variable within Java program. Here are few points which is worth noting :
1) Use organization properties if value of surround variable is available via system property e.g. Username which is available using "user.name" organization property. If you lot access it using surround variable straight you lot may postulate to enquire for dissimilar variable equally it may live on dissimilar inwards Windows e.g. USERNAME in addition to Unix equally USER.
2) Environment variables are case sensitive inwards Unix acre instance insensitive inwards Windows then relying on that tin forcefulness out ane time to a greater extent than brand your Java plan platform dependent.
3) System.getEnv() was deprecated inwards liberate JDK 1.3 inwards back upward of using System.getProperty() but reinstated ane time to a greater extent than inwards JDK 1.5.
That's all on how to larn surround variable inwards Java. Though you lot accept convenient method similar System.getEnv() which tin forcefulness out render value of surround variable, its amend to role System.getProperty() to larn that value inwards a platform independent way, if that surround variable is available equally organization holding inwards Java.
Further Learning
Complete Java Masterclass
How to convert Enum to String inwards Java
0 Response to "How To Buy The Farm Surroundings Variables Inwards Java- Illustration Tutorial"
Post a Comment