forked from Apress/learn-java-for-android-dev-14
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExec.java
More file actions
36 lines (35 loc) · 970 Bytes
/
Copy pathExec.java
File metadata and controls
36 lines (35 loc) · 970 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.io.InputStream;
import java.io.IOException;
public class Exec
{
public static void main(String[] args)
{
if (args.length != 1)
{
System.err.println("usage: java Exec program");
return;
}
try
{
Process p = Runtime.getRuntime().exec(args[0]);
// Obtaining process standard output.
InputStream is = p.getInputStream();
int _byte;
while ((_byte = is.read()) != -1)
System.out.print((char) _byte);
// Obtaining process standard error.
is = p.getErrorStream();
while ((_byte = is.read()) != -1)
System.out.print((char) _byte);
System.out.println("Exit status: " + p.waitFor());
}
catch (InterruptedException ie)
{
assert false; // should never happen
}
catch (IOException ioe)
{
System.err.println("I/O error: " + ioe.getMessage());
}
}
}