Home > Tutorial > Java Environment Variables Sample Code

Java Environment Variables Sample Code

Many Java applications will query the running environment for variables that are needed for the Java process. This can be done on a windows system or on something like Linux if that is your working platform. For our purposes, I will just focus on a windows environment, but the process is simple on Linux. In your Java application start shell script, you can set the environment variables (export MY_CONFIG_FILE=/path/to/config/file ) very simply.

In order to query the environment for a variable, you first need to set that variable. On a windows environment, there are many set by default, but you will need to add your own. This can be done in Eclipse.

Click on Run >> Run Configurations >> Environment >> New and then enter the Name and Value. The Name is the name of the variable and the Value is what that variable needs to be set to. Repeat this process for each environment value you need to define and then finish by click Apply. You should see something like this….

Java Environment Variables Source Code

package com.jcd.java.tutorials;

import java.util.Map;

public class JavaEnvironmentVariables 
{
	
	//Store all environment variables in Java Map
	static Map<String,String> env = System.getenv();

	public static void main(String[] args) {
		
		String MY_CONFIG_FILE = "MY_CONFIG_FILE";
		String MY_OUTPUT_FILE = "MY_OUTPUT_FILE";

		//Check for and pull MY_CONFIG_FILE environment variable
		if(env.containsKey(MY_CONFIG_FILE)) 
		{
			System.out.println("\nEnv Config File: "+env.get(MY_CONFIG_FILE));
		}
		else 
		{
			System.out.println("\nDid Not Find: "+MY_CONFIG_FILE);
		}
		
		//Check for and pull MY_OUTPUT_FILE environment variable
		if(env.containsKey(MY_OUTPUT_FILE)) 
		{
			System.out.println("\nEnv Output File: "+env.get(MY_OUTPUT_FILE));
		}
		else 
		{
			System.out.println("\nDid Not Find: "+MY_OUTPUT_FILE);
		}
		
		
		//Print out all of the environment variables that are set
		System.out.println("\nAll Environment Variables: "+env.keySet());

	}

}


Sample Java Environment Variables Output

Env Config File: C:\Users\Admin\Workspace\conf.txt

Env Output File: C:\Users\Admin\Workspace\out.txt

All Environment Variables: [GOOGLE_DEFAULT_CLIENT_ID, USERDOMAIN_ROAMINGPROFILE, LOCALAPPDATA, PROCESSOR_LEVEL, USERDOMAIN, FPS_BROWSER_APP_PROFILE_STRING, LOGONSERVER, PROMPT, SESSIONNAME, ALLUSERSPROFILE, MY_CONFIG_FILE, PROCESSOR_ARCHITECTURE, GOOGLE_DEFAULT_CLIENT_SECRET, PSModulePath, SystemDrive, OneDrive, APPDATA, USERNAME, ProgramFiles(x86), GOOGLE_API_KEY, CommonProgramFiles, Path, FPS_BROWSER_USER_PROFILE_STRING, PATHEXT, OS, COMPUTERNAME, PROCESSOR_REVISION, CommonProgramW6432, ComSpec, ProgramData, ProgramW6432, HOMEPATH, SystemRoot, TEMP, HOMEDRIVE, PROCESSOR_IDENTIFIER, USERPROFILE, TMP, CommonProgramFiles(x86), ProgramFiles, PUBLIC, NUMBER_OF_PROCESSORS, windir, MY_OUTPUT_FILE]



Java References:

  1. Java System Class
  2. Java HashMap Sample Code
  3. Java HashMap Sort By Value
  4. Java HashMap Sort By Key
Smedley
Posted by

Smedley

Smedley has been programming in Java for about twenty years. He enjoys sharing code samples to other fellow Java coders.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *