• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

ProcessBuilder

July 13, 2012 By j2eereference Leave a Comment

ProcessBuilder class is introduced under the package java.lang in java 1.5 ,

This provides a way to start and manage processes.

How to use process builder ?

Step 1 : Create the instance of ProcessBuilder with program name as parameter .

There are two ways to create the ProcessBuilder Objects

ProcessBuilder processBuilder= new ProcessBuilder(List<String> args)

ProcessBuilder processBuilder= new ProcessBuilder(String…  args)

In the first case we are passing a list type as a parameters

In the second case we are passing parameter through varargs , Click here to know more about varargs in java

Step 2 : Call start() method on the instance.

Now lets see one example code to start the text editor notepad .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.j2eereference.coreJava;
 
public class ProcessBuilderDemo {
 
public static void main(String[] args) {
   try{
          ProcessBuilder pbuilder= new ProcessBuilder("notepad.exe");
          pbuilder.start();
      }
  catch (Exception e) {
      e.printStackTrace();
   }
}
}

Output

Notepad will be opened.

In the above example it is possible to pass other parameter like , file name

1
ProcessBuilder pbuilder= new ProcessBuilder("notepad.exe" ,"myFile");

This will open a txt file named as myFile

Getting the environment settings of the current process.

We can get all the  environment setting details like user details, pathtext etc. using the method environment()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.j2eereference.corejava;
 
public class ProcessBuilderDemo {
 
 public static void main(String[] args) {
   try{
    ProcessBuilder pbuilder= new ProcessBuilder("notepad.exe");
    System.out.println(pbuilder.environment());
    pbuilder.start();
  }
   catch (Exception e) {
     e.printStackTrace();
    }
  }
}

 

Clearing the environment settings of the current process.

There is another method called clear() to clear all the environment details of the current process

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.j2eereference.corejava;
 
public class ProcessBuilderDemo {
 
public static void main(String[] args) {
  try{
    ProcessBuilder pbuilder= new ProcessBuilder("notepad.exe");
    System.out.println("Before clearing :"+pbuilder.environment());
    pbuilder.environment().clear();
    System.out.println("After clearing : "+pbuilder.environment());
    pbuilder.start();
}
  catch (Exception e) {
    e.printStackTrace();
   }
}
}

 

Adding new environment details for the current process

We can add any number of environment details for the current  process .

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.j2eereference.coreJava;
 
public class ProcessBuilderDemo {
 
public static void main(String[] args) {
   try{
     ProcessBuilder pbuilder= new ProcessBuilder("notepad.exe");
     pbuilder.environment().clear();
     pbuilder.environment().put("JAVA_HOME","C:\\Program Files\\Java\\jdk1.6.0_33");
     System.out.println("After clearing : "+pbuilder.environment());
     pbuilder.start();
  }
catch (Exception e) {
   e.printStackTrace();
   }
}
}

Output

After clearing : {JAVA_HOME=C:\Program Files\Java\jdk1.6.0_33}

ProcessBuilder vs runtime.exec

ProcessBuilder offers more control over the processes like setting the current working directory, changing the environment variables.

Related Posts

  • UncaughtExceptionHandler in java
  • How to generate and resolve OutOfMemoryError?
  • Difference between Spring’s Singleton scope & Singleton Design pattern
  • How to stop a thread?
  • Interrupting a thread in java
  • What is ThreadLocal in Java ?
  • ArrayList custom Implementation
  • Difference between volatile and synchronized keyword?
  • How to write thread safe code?
  • How to avoid deadlock in java ?

Filed Under: Core Java

Reader Interactions

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.