Quantcast
Channel: Crunchify
Viewing all articles
Browse latest Browse all 1037

In Java How to Print all Environment Properties value using ProcessBuilder?

$
0
0

Java Print all Environment Variable values using ProcessBuilder

When you run Java Program locally in Terminal Window or in Eclipse IDE, there are lots of Environment Variable which plays key role during program execution.

Here are list of Environment properties for your reference:

  1. PATH
  2. JAVA_STARTED_ON_FIRST_THREAD_78766
  3. SHELL
  4. USER
  5. TMPDIR
  6. APP_ICON_78766
  7. SSH_AUTH_SOCK
  8. JAVA_MAIN_CLASS_80856
  9. XPC_FLAGS
  10. __CF_USER_TEXT_ENCODING
  11. Apple_PubSub_Socket_Render
  12. LOGNAME
  13. XPC_SERVICE_NAME
  14. HOME DIRECTORY

All of these values are used and critical to any Java Program.

In this tutorial we will go over how to load and print all of these values using ProcessBuilder class. Here is a brief details on java.lang.ProcessBuilder Class.

Each ProcessBuilder instance manages a collection of process attributes

Program Logic:

  • Create class CrunchifyGetProcessEnvironment.java.
  • Create ProcessBuilder Java Object.
  • Use method environment() which returns a string map view of this process builder’s environment.
  • Iterate through Java Map and print all values.
  • Here in this program we are spawning multiple threads and using method executor.execute() method.

 CrunchifyGetProcessEnvironment,java

package crunchify.com.tutorial;

import java.io.File;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author Crunchify.com 
 * Program: Simplest way to get ProcessBuilder Environment Information. 
 * Java Version: 1.0.2
 * 
 */

public class CrunchifyGetProcessEnvironment {

	public static void main(String a[]) {

		ProcessBuilder crunchifyProcess = new ProcessBuilder();

		// java executor.execute Tutorial
		// java spawn multiple threads
		// Java - Creating Multiple Threads with a For Loop - just to check what we are getting as Thread Value
		ExecutorService executor = Executors.newCachedThreadPool();
		for (int i = 1; i <= 5; i++)
			executor.execute(new crunchifyRunnableMethod(i));

		// environment() returns a string map view of this process builder's environment. Whenever a process builder is created, the
		// environment is initialized to a copy of the current process environment
		Map<String, String> crunchifyEnvironment = crunchifyProcess.environment();

		Set<String> myKeys = crunchifyEnvironment.keySet();
		println("\n================= Printing ProcessBuilder Environment Properties =================");
		for (String aKey : myKeys) {
			println(aKey + " \t==> " + crunchifyEnvironment.get(aKey));
		}

		// Set Directory
		File newDir = new File("~/ashah/Desktop");
		crunchifyProcess.directory(newDir);

		// Get working directory. directory() returns this process builder's working directory.
		File directory = crunchifyProcess.directory();
		println("\nThis is what we set as working directory: " + directory);
	}

	public static void println(String string) {
		System.out.println(string);
	}

}

// Runnable Class which we specified in executor.execute() method above
class crunchifyRunnableMethod implements Runnable {

	public crunchifyRunnableMethod(int i) {
		System.out.println(i);
	}

	// executor.execute() calls this method every time we call it.
	@Override
	public void run() {
		System.out.println("Running Overriding run() method...");
	}

}

Just run program as Java Application and you should see below similar result.

Eclipse Console Output:

1
2
Running Overriding run() method...
3
Running Overriding run() method...
Running Overriding run() method...
4
5
Running Overriding run() method...
Running Overriding run() method...

================= Printing ProcessBuilder Environment Properties =================
PATH 	==> /usr/bin:/bin:/usr/sbin:/sbin
JAVA_STARTED_ON_FIRST_THREAD_78766 	==> 1
SHELL 	==> /bin/bash
USER 	==> ashah
TMPDIR 	==> /var/folders/8s/6y8s_v856rldwwfgbkwy2kqc0000gp/T/
APP_ICON_78766 	==> ../Resources/Eclipse.icns
SSH_AUTH_SOCK 	==> /private/tmp/com.apple.launchd.6IM0Oo9svW/Listeners
JAVA_MAIN_CLASS_80856 	==> crunchify.com.tutorial.CrunchifyGetProcessEnvironment
XPC_FLAGS 	==> 0x0
__CF_USER_TEXT_ENCODING 	==> 0x1F6:0x0:0x0
Apple_PubSub_Socket_Render 	==> /private/tmp/com.apple.launchd.JMbJD0NVsS/Render
LOGNAME 	==> ashah
XPC_SERVICE_NAME 	==> org.eclipse.platform.ide.6232
HOME 	==> /Users/ashah

This is what we set as working directory: ~/ashah/Desktop

Hope you get complete idea on what all environment properties linked to a specific Java program.

The post In Java How to Print all Environment Properties value using ProcessBuilder? appeared first on Crunchify.


Viewing all articles
Browse latest Browse all 1037

Latest Images

Trending Articles



Latest Images