-
-
Save cb372/2224509 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 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.LogOutputStream; | |
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 { | |
//プロセスの出力をLogOutputStreamに吐かせる | |
LogOutputStream output = new LogOutputStream() { | |
@Override | |
protected void processLine(String line, int level) { | |
System.out.println(line); | |
} | |
}; | |
PumpStreamHandler streamHandler = new PumpStreamHandler(output); | |
executor.setStreamHandler(streamHandler); | |
// 正常終了の場合に返される値 | |
executor.setExitValue(0); | |
// 非同期で実行 | |
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); | |
executor.execute(commandLine, resultHandler); | |
// TODO output.close()しなければならない? | |
} catch (ExecuteException ex) { | |
ex.printStackTrace(); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
charset problem?