Use of environment variable in Java
In this post we will talk about environment variables in java , their role and how to use them in windows and Unix environment.
Reading environment Variables in Java
System environment variable can be used in the java application to set the system related values for ex: application path, database environment etc..,
In standalone application, start the JVM with the “-D” switch to pass properties to the application and read them with the System.getProperty() method.
SET myvar=Hello world
SET myothervar=nothing
java -Dmyvar=”%myvar%” -Dmyothervar=”%myothervar%” myClass
Then in myClass do the following
String myvar = System.getProperty(“myvar”);
String myothervar = System.getProperty(“myothervar”);
Reading UNIX environment Variables in Java
How the code works:
Create an instance of a java.util.Properties class.
Every Java application has a single Runtime instance, which is an interface with the environment in which the application is running. The current Runtime is obtained using the static method getRuntime() of Runtime class.
Call the exec method of the Runtime object, to execute the given command in a separate process.
After executing, the method returns the handle to a Process class. Get the input stream for the sub process using getInputStream() method.
Load the Properties object with this InputStream.
Now we can read any environment variable form the Properties Object, by using get method.
Lines of Code:
java.util.Properties env = new java.util.Properties();
env.load(Runtime.getRuntime().exec(“env”).getInputStream());
String myEnvVar =(String) env.get(“MY_VAR”);
Configuring / Reading the JVM environment variable in IBM WAS Server.
JVM environment variables can be set in the WAS server, and that environment variable can be read from the java program using System.getProperty().
In a real time scenario, database environment variable can be set in the JVM custom properties, and that can be changed depending on which environment the application is deployed.
For ex: for production database environment, value can be set to PROD so that production database related properties file can be read by the application.
Following is the path in the WAS server where we need to set the JVM custom variable and value.
Below is the sample snapshot which shows the DB_ENVIRONMENT property name and value settings.
After adding a new value or updating the value, restart the WAS server, so that changes will get reflected in the application.
Code to read the environment setup:
String env = System.getProperty(“DB_ENVIRONMENT”)
So depending on the value of env variable, we can read the properties file for different environment
For ex:
If(“DEVELOPMENT”.equals(env)) {
//Load the development environment related property files.
//Connect to development database.
} else If(“PROD”.equals(env)) {
//Load the production environment related property files.
//Connect to production database.
} If(“UAT”.equals(env)) {
//Load the uat environment related property files.
//Connect to uat database.
}









