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.
Leave a Reply