Printing the Windows environment variables and their values to System Console (and file) using Java

In the following Java tool, I have demonstrated two things:

  • How to print all the Windows Variables using Java runtime onto the System Console. (You can modify this code to print those variables and their values to a file. See our previous tutorial for some help on printing String to file)
  • How to get the value of a specific windows variable using Java runtime

This program is capable of identifying few variations of Windows OS. The following OS are supported by this code:

  • Windows NT
  • Windows 2000
  • Windows XP
  • Windows 9* (e.g. Windows 95, Windows 98

Here is the source code:

package com.kushal.tools;

/**
 * @Author Kushal Paudyal
 * Created on 2012-01-30
 * Last Modified on 2012-01-30
 *
 * Tool to print the windows environment variables.
 * Can also be used to print the value of a specific
 * windows variable.
 */
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Properties;

public class WindowsEnvironmentVariablesPrinter {

	public static void main(String[] args) {

		Properties prop = WindowsEnvironmentVariablesPrinter
				.getEnvironmentVariables();
		/**
		 * Print out all the properties to System Console
		 */
		prop.list(System.out);

		/**
		 * Print the value of a specific variable.
		 * In this case, we will be printing the value of "TEMP"
		 * variable.
		 */
		System.out.println("nnValue of varible TEMP is : "
				+ prop.getProperty("TEMP"));
	}

	public static Properties getEnvironmentVariables() {
		Process myProcess = null;
		Properties envVars = new Properties();
		try {
			/**Get a runtime**/
			Runtime runtime = Runtime.getRuntime();

			/**Get the String value of operating system name*/
			String operatingSystem = System.getProperty("os.name").toLowerCase();
			/**
			 * If it is Windows 9 Operating System, execute the command
			 * 'command.com /c set'
			 */
			if (operatingSystem.indexOf("windows 9") > -1) {
				myProcess = runtime.exec("command.com /c set");
			}
			/**
			 * If it is Windows NT or Windows 2000 or Windows XP, execute the command
			 * 'cmd.exe /c set'
			 */
			else if ((operatingSystem.indexOf("nt") > -1)
					|| (operatingSystem.indexOf("windows 2000") > -1)
					|| (operatingSystem.indexOf("windows xp") > -1)) {
				myProcess = runtime.exec("cmd.exe /c set");
			}
			/**
			 * If does not match any of those, just run the command
			 * 'env'
			 */
			else {
				myProcess = runtime.exec("env");
			}

			/**
			 * The following block reads the output and creates a
			 * key value pair for the environment variables.
			 */
			BufferedReader br = new BufferedReader(new InputStreamReader(
					myProcess.getInputStream()));
			String line;
			while ((line = br.readLine()) != null) {
				int idx = line.indexOf('=');
				String key = line.substring(0, idx);
				String value = line.substring(idx + 1);
				envVars.setProperty(key, value);
			}
		} catch (Throwable e) {
			e.printStackTrace();
		}
		return envVars;
	}

}

 

The output of this program when I ran this on my PC:

-- listing properties --
UATDATA=C:WINXPsystem32CCMUATDataD9F8C39...
CLIENTNAME=Console
PROCESSOR_ARCHITECTURE=x86
NUT_SUFFIXED_SEARCHING=1
TSMPATH=C:Program FilesThinkPadUltraNav Ut...
FP_NO_HOST_CHECK=NO
RATL_RTHOME=C:Program FilesRationalRational Test
DB2TEMPDIR=C:PROGRA~1IBMSQLLIB
ANT_HOME=C:build2toolsapache-ant-1.7.1
TVTPYDIR=C:Program FilesCommon FilesLenovo...
MQ_JAVA_INSTALL_PATH=C:Program FilesIBMWebSphere MQJava
TMP=C:DOCUME~1billgatesLOCALS~1Temp
TVT=C:Program FilesLenovo
CLEARCASE_PRIMARY_GROUP=NWIEUG$ICAClearUsers
COMPUTERNAME=BILLGATESCOMPUTER
M2=C:mavenbin
OS=Windows_NT
IBMLDAP_ALTHOME=C:Program FilesRationalcommoncodeset
RR=C:Program FilesLenovoRescue and Re...
PROMPT=$P$G
notestmp1=C:DOCUME~1billgatesLOCALS~1Tempnot...
SystemDrive=C:
TPTP_AC_HOME=C:Program FilesIBMSDPSharedplugin...
HOMEDRIVE=C:
LOGONSERVER=\PMM0201
PROCESSOR_IDENTIFIER=x86 Family 6 Model 37 Stepping 5, Gen...
ProgramFiles=C:Program Files
DB2INSTANCE=DB2
NUMBER_OF_PROCESSORS=4
TEMP=C:DOCUME~1billgatesLOCALS~1Temp
USERDOMAIN=MSOFT
M2_HOME=C:maven
INCLUDE=C:PROGRA~1IBMSQLLIBINCLUDE;C:PRO...
NUTSUFFIX=1
HOME=C:Documents and Settingsbillgates
MQ_JAVA_DATA_PATH=C:Program FilesIBMWebSphere MQ
TMPDIR=C:DOCUME~1billgatesLOCALS~1Temp
SMA=C:Program FilesThinkVantageSMA
IBM_JAVA_COMMAND_LINE=C:Program FilesIBMSDPjdkjrebin...
PROCESSOR_LEVEL=6
USERDNSDOMAIN=NWIE.NET
VSEDEFLOGDIR=C:Documents and SettingsAll UsersA...
Path=C:Program FilesIBMWebSphere MQJav...
SESSIONNAME=Console
USERNAME=billgates
LIB=;C:PROGRA~1IBMSQLLIBLIB
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JS...
DEFLOGDIR=C:Documents and SettingsAll UsersA...
ComSpec=C:WINXPsystem32cmd.exe
SystemRoot=C:WINXP
TVTCOMMON=C:Program FilesCommon FilesLenovo
windir=C:WINXP
PROBEKIT_HOME=C:Program FilesIBMSDPSharedplugin...
PROCESSOR_REVISION=2505
USERPROFILE=C:Documents and Settingsbillgates
CommonProgramFiles=C:Program FilesCommon Files
APPDATA=C:Documents and SettingsbillgatesApp...
HOMEPATH=Documents and Settingsbillgates
TISDIR=C:Program FilesRationalcommon
JAVA_HOME=C:Program FilesIBMSDPruntimesbas...
ALLUSERSPROFILE=C:Documents and SettingsAll Users
CLASSPATH=C:Program FilesIBMWebSphere MQJav...
SWSHARE=C:SWSHARE

Value of varible TEMP is : C:DOCUME~1billgatesLOCALS~1Temp