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("\n\nValue 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:\WINXP\system32\CCM\UATData\D9F8C39...
CLIENTNAME=Console
PROCESSOR_ARCHITECTURE=x86
NUT_SUFFIXED_SEARCHING=1
TSMPATH=C:\Program Files\ThinkPad\UltraNav Ut...
FP_NO_HOST_CHECK=NO
RATL_RTHOME=C:\Program Files\Rational\Rational Test
DB2TEMPDIR=C:\PROGRA~1\IBM\SQLLIB\
ANT_HOME=C:\build2\tools\apache-ant-1.7.1
TVTPYDIR=C:\Program Files\Common Files\Lenovo\...
MQ_JAVA_INSTALL_PATH=C:\Program Files\IBM\WebSphere MQ\Java
TMP=C:\DOCUME~1\billgates\LOCALS~1\Temp
TVT=C:\Program Files\Lenovo
CLEARCASE_PRIMARY_GROUP=NWIE\UG$ICAClearUsers
COMPUTERNAME=BILLGATESCOMPUTER
M2=C:\maven\bin
OS=Windows_NT
IBMLDAP_ALTHOME=C:\Program Files\Rational\common\codeset
RR=C:\Program Files\Lenovo\Rescue and Re...
PROMPT=$P$G
notestmp1=C:\DOCUME~1\billgates\LOCALS~1\Temp\not...
SystemDrive=C:
TPTP_AC_HOME=C:\Program Files\IBM\SDPShared\plugin...
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~1\billgates\LOCALS~1\Temp
USERDOMAIN=MSOFT
M2_HOME=C:\maven
INCLUDE=C:\PROGRA~1\IBM\SQLLIB\INCLUDE;C:\PRO...
NUTSUFFIX=1
HOME=C:\Documents and Settings\billgates
MQ_JAVA_DATA_PATH=C:\Program Files\IBM\WebSphere MQ
TMPDIR=C:\DOCUME~1\billgates\LOCALS~1\Temp
SMA=C:\Program Files\ThinkVantage\SMA\
IBM_JAVA_COMMAND_LINE=C:\Program Files\IBM\SDP\jdk\jre\bin\...
PROCESSOR_LEVEL=6
USERDNSDOMAIN=NWIE.NET
VSEDEFLOGDIR=C:\Documents and Settings\All Users\A...
Path=C:\Program Files\IBM\WebSphere MQ\Jav...
SESSIONNAME=Console
USERNAME=billgates
LIB=;C:\PROGRA~1\IBM\SQLLIB\LIB
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JS...
DEFLOGDIR=C:\Documents and Settings\All Users\A...
ComSpec=C:\WINXP\system32\cmd.exe
SystemRoot=C:\WINXP
TVTCOMMON=C:\Program Files\Common Files\Lenovo
windir=C:\WINXP
PROBEKIT_HOME=C:\Program Files\IBM\SDPShared\plugin...
PROCESSOR_REVISION=2505
USERPROFILE=C:\Documents and Settings\billgates
CommonProgramFiles=C:\Program Files\Common Files
APPDATA=C:\Documents and Settings\billgates\App...
HOMEPATH=\Documents and Settings\billgates
TISDIR=C:\Program Files\Rational\common
JAVA_HOME=C:\Program Files\IBM\SDP\runtimes\bas...
ALLUSERSPROFILE=C:\Documents and Settings\All Users
CLASSPATH=C:\Program Files\IBM\WebSphere MQ\Jav...
SWSHARE=C:\SWSHARE


Value of varible TEMP is : C:\DOCUME~1\billgates\LOCALS~1\Temp
Blog Widget by LinkWithin

Originally posted 2012-02-16 19:02:41.

Share