Java by API/java.lang/Process — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 17:43, 31 мая 2010
Содержание
Process: exitValue()
/*
* Output:
*
notepad returned 0
*
*
*/
public class MainClass {
public static void main(String args[]) {
Runtime r = Runtime.getRuntime();
Process p = null;
String cmd[] = { "notepad", "/java/src/java/lang/Runtime.java" };
try {
p = r.exec(cmd);
p.waitFor();
} catch (Exception e) {
System.out.println("error executing " + cmd[0]);
}
System.out.println(cmd[0] + " returned " + p.exitValue());
}
}
Process: getInputStream()
import java.io.BufferedReader;
import java.io.InputStreamReader;
/*
*/
public class MainClass {
public static void main(String[] args) throws Exception{
Runtime r = Runtime.getRuntime();
String[] nargs = { "sh", "-c", "for i in 1 2 3; do echo $i; done" };
Process p = r.exec(nargs);
BufferedReader is = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while ((line = is.readLine()) != null)
System.out.println(line);
}
}
Process: getOutputStream()
import java.io.OutputStream;
public class Main {
public static void main(String[] argv) throws Exception {
String command = "cat";
Process child = Runtime.getRuntime().exec(command);
OutputStream out = child.getOutputStream();
out.write("some text".getBytes());
out.close();
}
}
Process: waitFor()
/*
* Output:
*
notepad returned 0
*
*
*/
public class MainClass {
public static void main(String args[]) {
Runtime r = Runtime.getRuntime();
Process p = null;
String cmd[] = { "notepad", "/java/src/java/lang/Runtime.java" };
try {
p = r.exec(cmd);
p.waitFor();
} catch (Exception e) {
System.out.println("error executing " + cmd[0]);
}
System.out.println(cmd[0] + " returned " + p.exitValue());
}
}