String command = "command of the operating system"; Process process = Runtime.getRuntime().exec(command); // deal with OutputStream to send inputs process.getOutputStream(); // deal with InputStream to get ordinary outputs process.getInputStream(); // deal with ErrorStream to get error outputs process.getErrorStream();Now, let’s walk through some real code examples.The following code snippet runs the ping command on Windows and captures its output:
String command = "ping www.codejava.net"; try { Process process = Runtime.getRuntime().exec(command); BufferedReader reader = new BufferedReader( new InputStreamReader(process.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } catch (IOException e) { e.printStackTrace(); }It gives the following output in the standard console:
Pinging codejava.net [198.57.151.22] with 32 bytes of data: Reply from 198.57.151.22: bytes=32 time=227ms TTL=51 Reply from 198.57.151.22: bytes=32 time=221ms TTL=51 Reply from 198.57.151.22: bytes=32 time=220ms TTL=51 Reply from 198.57.151.22: bytes=32 time=217ms TTL=51 Ping statistics for 198.57.151.22: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 217ms, Maximum = 227ms, Average = 221ms
BufferedReader reader = new BufferedReader( new InputStreamReader(process.getInputStream()));Then invoke the readLine() method of the reader to read the output line by line, sequentially:
String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close();
Scanner scanner = new Scanner(process.getInputStream()); scanner.useDelimiter("\r\n"); while (scanner.hasNext()) { System.out.println(scanner.next()); } scanner.close();
BufferedReader errorReader = new BufferedReader( new InputStreamReader(process.getErrorStream())); while ((line = errorReader.readLine()) != null) { System.out.println(line); } errorReader.close();So it’s recommended to capture both the standard output and error output to handle both normal and abnormal cases.
String command = "cmd /c date"; try { Process process = Runtime.getRuntime().exec(command); BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(process.getOutputStream())); writer.write("09-20-14"); writer.close(); BufferedReader reader = new BufferedReader(new InputStreamReader( process.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } catch (IOException e) { e.printStackTrace(); }Output:
The current date is: Sat 09/20/2014 Enter the new date: (mm-dd-yy) 09-20-14Check the system clock, it is updated immediately.
int exitValue = process.waitFor(); if (exitValue != 0) { System.out.println("Abnormal process termination"); }Note that the waitFor() method returns an integer value indicating whether the process terminates normally (value 0) or not. So it’s necessary to check this value.
process.destroy(); if (process.exitValue() != 0) { System.out.println("Abnormal process termination"); }NOTE: Using the waitFor() and exitValue() method is exclusive, meaning that either one is used, not both.
exec(String[] cmdarray)
This method is useful to execute a command with several arguments, especially arguments contain spaces. For example, the following statements execute a Windows command to list content of the Program Files directory:String commandArray[] = {"cmd", "/c", "dir", "C:\\Program Files"}; Process process = Runtime.getRuntime().exec(commandArray);For other exec() methods, consult the relevant Javadoc which is listed below.