public class OSExecuteException extends RuntimeException {
private static final long serialVersionUID = 5901555192879431127L;
public OSExecuteException(String msg){
super(msg);
}
}
-----------------------
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
*
* 进程控制
*
*/
public class OSExecute {
public static void command(String command) {
boolean err = false;
try {
Process proc = new ProcessBuilder(command.split(" ")).start();
BufferedReader reader = new BufferedReader(new InputStreamReader(
proc.getInputStream()));
String s = null;
while ((s = reader.readLine()) != null) {
System.out.println(s);
}
reader.close();
BufferedReader errReader = new BufferedReader(
new InputStreamReader(proc.getErrorStream()));
// 如果进程执行出错,则输出错误信息
while ((s = errReader.readLine()) != null) {
System.out.println(s);
err = true;
}
errReader.close();
} catch (Exception e) {
// 处理win2K默认抛出的异常
if (!command.startsWith("CMD /C")) {
command("CMD /C " + command);
} else {
throw new RuntimeException(e);
}
}
if (err) {
throw new OSExecuteException("Errors executing " + command);
}
}
public static void main(String[] args) {
OSExecute.command("javap OSExecute");
}
}
[java][io]进程控制
最新推荐文章于 2024-01-09 12:06:09 发布