Checkout for Promo Codes

Program: How to get process environment variables in java at runtime?


The ProcessBuilder.environment() method returns you the ProcessBuilder's environemnt. Below example shows how to read it.

package com.java2novice.processbuilder;

import java.util.Map;
import java.util.Set;

public class MyEnvDetails {

	public static void main(String a[]){
		ProcessBuilder pb = new ProcessBuilder();
		Map<String, String> envMap = pb.environment();
		Set<String> keys = envMap.keySet();
		for(String key:keys){
			System.out.println(key+" ==> "+envMap.get(key));

SHELL ==> /bin/bash
JAVA_MAIN_CLASS_37131 ==> com.java2novice.processbuilder.MyEnvDetails
APP_ICON_385 ==> ../Resources/Eclipse.icns ==> client
TMPDIR ==> /var/folders/2n/yf7x2fdx3bv6g31hn4stv1y0_qsrzm/T/
__CF_USER_TEXT_ENCODING ==> 0x157CE3F4:0:0
PATH ==> /usr/bin:/bin:/usr/sbin:/sbin
COMMAND_MODE ==> unix2003
DISPLAY ==> /tmp/launch-DwnV41/org.x:0
USER ==> root ==>
Apple_Ubiquity_Message ==> /tmp/launch-u3pr6e/Apple_Ubiquity_Message
LOGNAME ==> root
Apple_PubSub_Socket_Render ==> /tmp/launch-nboOhX/Render
<< Previous Program | Next Program >>

List Of All ProcessBuilder Class Sample Programs:

  1. How to invoke other applicatons in java?
  2. How to run operating system specific command and read its output?
  3. How to get process environment variables in java at runtime?
  4. How to run ProcessBuilder with list of commands?
Knowledge Centre
Transient and Volatile Modifiers
Transient: The transient modifier applies to variables only and it is not stored as part of its object's Persistent state. Transient variables are not serialized.

Volatile: Volatile modifier applies to variables only and it tells the compiler that the variable modified by volatile can be changed unexpectedly by other parts of the program.
Famous Quotations
It’s not that I’m so smart, it’s just that I stay with problems longer.
-- Albert Einstein

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2020 by Nataraja Gootooru. All Rights Reserved.