Using the new Process Builder class

We all know how to execute programs from within a Java Application by making use of the Runtime Api. For example, assume that we want to launch the Internet Explorer browser within the Java code. Then the following code snippet will just do that,

also read:

ProcessLauncher.java

    package tips.process;
 
    import java.io.*;
 
    public class ProcessLauncher {
 
        public static void main(String[] args) throws Exception {
 
            String[] commandArray = {'javap' , '-private',
                'java.lang.String'};
            String[] environment = {'path=;',
                'path=D:\Java\Java6.0\bin;'};
 
            Runtime runtime = Runtime.getRuntime();
            Process javap = runtime.exec(commandArray, environment);
            writeProcessOutput(javap);
        }
 
        static void writeProcessOutput(Process process) throws Exception{
            InputStreamReader tempReader = new InputStreamReader(
                new BufferedInputStream(process.getInputStream()));
            BufferedReader reader = new BufferedReader(tempReader);
            while (true){
                String line = reader.readLine();
                if (line == null)
                    break;
                System.out.println(line);
            }
        }
    }

The above program makes use of java.lang.Runtime class for setting all the environmental options and the various command arguments. For example, in the above case for executing the javap.exe command, which comes as part of the JDK distribution, it is necessary to set the environment path variable. To achieve this, the environment array is well populated with the path variable. And to list down all the private members of some class, the javap command should be passed with ‘;-private'; option. The rest of the code executes the command by calling the Runtime.exec() command and outputs the contents back to the console. Now, let us see how to achieve the same effect using the new Process Builder class.

ProcessBuilderTest.java

    package tips.process;
 
    import java.io.*;
    import java.util.*;
 
    public class ProcessBuilderTest {
 
        public static void main(String[] args) throws Exception {
 
            ProcessBuilder builder = new ProcessBuilder('javap', '-private',
                  'java.lang.String');
            Map<String, String> environment = builder.environment();
 
            environment.put('path', ';'); // Clearing the path variable;
            environment.put('path', 'D:\Java\Java6.0\bin;');
 
            Process javap = builder.start();
            writeProcessOutput(javap);
        }
 
        static void writeProcessOutput(Process process) throws Exception{
            ...
        }
    }

In the above program, we have used the Process Builder class to initiate the javap command. More specifically, we have taken advantage of the constructor by passing all the options along with the command name. Then, we have populated all the environmental variables in a map which was obtained by making a call to ProcessBuilder.environment(). This call returns a map of options that was set to the current Builder which got initialized internally by calling the System.getenv() method.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*