Java Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | import java.util.ArrayList; import java.util.List; public class JavaProcessBuilder { public static void main(String[] args) { // create a new list of arguments for our process List list = new ArrayList(); list.add("notepad.exe"); // create the process builder ProcessBuilder pb = new ProcessBuilder(list); // get the command list System.out.println("" + pb.command()); } } |
The ProcessBuilder class is used to create operating system processes.
In java we use two classes, the Runtime class and the Process class. Exec method of the Runtime class to run the command as a separate process. Invoking the exec method returns a Process object for managing the subprocess. getInputStream() and getErrorStream() are the two methods of the Process object to read the normal output of the command, and the error output of the command.