Skip to content

Instantly share code, notes, and snippets.

@cb372
Forked from sakamotodesu/ApacheCommonExec.java
Created March 28, 2012 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cb372/2224509 to your computer and use it in GitHub Desktop.
Save cb372/2224509 to your computer and use it in GitHub Desktop.
apache commons exec "write end dead" sample
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();
}
}
}
@honwhy
Copy link

honwhy commented Jan 12, 2016

charset problem?

@honwhy
Copy link

honwhy commented Jan 12, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment