★★Java質問・相談スレッド142★★
■ このスレッドは過去ログ倉庫に格納されています
0201デフォルトの名無しさん
2010/12/09(木) 20:25:15public class ProcessRunner {
public static String execute(String cmd) throws IOException, InterruptedException {
Process proc = Runtime.getRuntime().exec(cmd);
InputStream stdout = proc.getInputStream();
InputStream stderr = proc.getErrorStream();
byte[] iobuf = new byte[1024];
ByteArrayOutputStream bout = new ByteArrayOutputStream(8192);
while ( isAlive(proc) ) {
if ( stdout.available() != 0 ) {
int len = stdout.read(iobuf);
if ( len == -1 ) break;
bout.write(iobuf, 0, len);
}else if ( stderr.available() > 0 ) {
stderr.read(iobuf);
}else {
Thread.sleep(15L);
}
}
return new String(bout.toByteArray());
}
private static boolean isAlive(Process proc) {
try {
proc.exitValue();
}catch ( IllegalThreadStateException e ) { return true; }
return false;
}
}
■ このスレッドは過去ログ倉庫に格納されています