【发布时间】:2015-01-12 15:17:01
【问题描述】:
我想在提取一些数据时使用外部工具(通过行循环)。 为此,我首先使用 Runtime.getRuntime().exec() 来执行它。 但后来我的提取变得非常缓慢。所以我正在寻找一种可能性,在循环的每个实例中执行外部工具,使用相同的 shell 实例。
我发现,我应该使用 ProcessBuilder。但它还没有工作。
这是我测试执行的代码(已经从论坛中的答案输入):
public class ExecuteShell {
ProcessBuilder builder;
Process process = null;
BufferedWriter process_stdin;
BufferedReader reader, errReader;
public ExecuteShell() {
String command;
command = getShellCommandForOperatingSystem();
if(command.equals("")) {
return; //Fehler! No error handling yet
}
//init shell
builder = new ProcessBuilder( command);
builder.redirectErrorStream(true);
try {
process = builder.start();
} catch (IOException e) {
System.out.println(e);
}
//get stdout of shell
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
errReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
//get stdin of shell
process_stdin = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
System.out.println("ExecuteShell: Constructor successfully finished");
}
public String executeCommand(String commands) {
StringBuffer output;
String line;
try {
//single execution
process_stdin.write(commands);
process_stdin.newLine();
process_stdin.flush();
} catch (IOException e) {
System.out.println(e);
}
output = new StringBuffer();
line = "";
try {
if (!reader.ready()) {
output.append("Reader empty \n");
return output.toString();
}
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
return output.toString();
}
if (!reader.ready()) {
output.append("errReader empty \n");
return output.toString();
}
while ((line = errReader.readLine())!= null) {
output.append(line + "\n");
}
} catch (Exception e) {
System.out.println("ExecuteShell: error in executeShell2File");
e.printStackTrace();
return "";
}
return output.toString();
}
public int close() {
// finally close the shell by execution exit command
try {
process_stdin.write("exit");
process_stdin.newLine();
process_stdin.flush();
}
catch (IOException e) {
System.out.println(e);
return 1;
}
return 0;
}
private static String getShellCommandForOperatingSystem() {
Properties prop = System.getProperties( );
String os = prop.getProperty( "os.name" );
if ( os.startsWith("Windows") ) {
//System.out.println("WINDOWS!");
return "C:/cygwin64/bin/bash";
} else if (os.startsWith("Linux") ) {
//System.out.println("Linux!");
return"/bin/sh";
}
return "";
}
}
我想在另一个类中调用它,比如这个 Testclass:
public class TestExec{
public static void main(String[] args) {
String result = "";
ExecuteShell es = new ExecuteShell();
for (int i=0; i<5; i++) {
// do something
result = es.executeCommand("date"); //execute some command
System.out.println("result:\n" + result); //do something with result
// do something
}
es.close();
}
}
我的问题是,输出流总是空的:
ExecuteShell: Constructor successfully finished
result:
Reader empty
result:
Reader empty
result:
Reader empty
result:
Reader empty
result:
Reader empty
我在这里阅读了帖子:Java Process with Input/Output Stream
但是代码 sn-ps 不足以让我继续前进,我错过了一些东西。我并没有真正使用不同的线程。而且我不确定扫描仪是否/如何对我有任何帮助。非常感谢您的帮助。
最终,我的目标是重复调用外部命令并使其快速。
编辑: 我改变了循环,使 es.close() 在外面。我想补充一点,我不希望只有这个在循环中。
编辑: 时间的问题是,我调用的命令导致了错误。当命令没有报错时,时间是可以接受的。
感谢您的回答
【问题讨论】:
标签: java processbuilder