Created
March 28, 2012 03:54
-
-
Save sakamotodesu/2223469 to your computer and use it in GitHub Desktop.
apache commons exec "write end dead" sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.test; | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
import java.io.PipedInputStream; | |
import java.io.PipedOutputStream; | |
import org.apache.commons.exec.CommandLine; | |
import org.apache.commons.exec.DefaultExecuteResultHandler; | |
import org.apache.commons.exec.DefaultExecutor; | |
import org.apache.commons.exec.ExecuteException; | |
import org.apache.commons.exec.PumpStreamHandler; | |
public class ApacheCommonExec { | |
public static void main(String[] args) { | |
// コマンドを作成 | |
CommandLine commandLine = new CommandLine("ping"); | |
commandLine.addArgument("/n"); | |
commandLine.addArgument("5"); | |
commandLine.addArguments("/w 1000"); | |
commandLine.addArgument("127.0.0.1"); | |
// Executorを作成 | |
DefaultExecutor executor = new DefaultExecutor(); | |
try { | |
//プロセスの出力をPipedOutputStreamに吐かせる | |
PipedOutputStream output = new PipedOutputStream(); | |
PumpStreamHandler streamHandler = new PumpStreamHandler(output); | |
executor.setStreamHandler(streamHandler); | |
// 正常終了の場合に返される値 | |
executor.setExitValue(0); | |
// 非同期で実行 | |
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); | |
executor.execute(commandLine, resultHandler); | |
PipedInputStream input = new PipedInputStream(output); | |
BufferedReader br = new BufferedReader(new InputStreamReader(input)); | |
String line = null; | |
while ((line = br.readLine()) != null) { | |
System.out.println(line); | |
} | |
output.close(); | |
input.close(); | |
} catch (ExecuteException ex) { | |
ex.printStackTrace(); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
} |
ありがとう!
やっぱapache common execの採用は見送った方がよさそうだね。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ここでApache Commons ExecもPipedInputStreamも悪い:
PipedOutputStreamのJavadocより:
PipedOutputStream#read()のJavadocより: