我想编写一个运行外部“java myprog < input.txt> output.txt”命令的Java程序。最终目标是在两个不同的程序上运行此命令,并比较它们各自输出文件的输出相似性。运行外部“Java myprog < input.txt > output.txt”的Java程序
我想我已经读了这篇关于使用的ProcessBuilder运行外部程序每一个相关的文章,以及关于外部程序处理用户输入的几个条目,但我仍然无法得到的东西的工作。从我读过的内容来看,我认为最好的方法是不运行上面的确切命令,而是读取input.txt文件,并逐字节地将它送入Process对象,然后收集输出并将其写入输出.txt ...我对其他选项100%开放。
我下面的代码放在一起基于我的读数。它似乎正确地将来自input.txt的输入馈送到myprog中,但是当我尝试将外部程序的输出打印到控制台进行验证时,程序在myprog中预期的(惊讶的)用户输入处挂起。
我得到同样的问题,有和没有redirectErrorStream(真)线。
我真的希望这是在Java中,因为我打算与大家分享其节目产出,我会比较人的源代码,他们主要是只熟悉Java。
import java.io.*;
import java.util.*;
public class test7 {
public static void main(String args[]) {
try {
// WANT: "java myprog <input.txt> output.txt"
String inputFile = "input.txt";
String outputFile = "output.txt";
ProcessBuilder pb = new ProcessBuilder("java","myprog");
pb.redirectErrorStream(true); // merge stdout, stderr of process
Process p = pb.start();
// write input to the running program
OutputStream pos = p.getOutputStream();
InputStream fis = new FileInputStream(inputFile);
int read = 0;
while ((read = fis.read()) != -1) {
pos.write(read);
}
fis.close();
// get output of running program
InputStreamReader isr = new InputStreamReader(p.getInputStream());
BufferedReader br = new BufferedReader(isr);
// HANGS HERE WHEN USER INPUT REQUIRED
String lineRead;
while ((lineRead = br.readLine()) != null) {
System.out.println(lineRead);
}
}
catch (IOException e) {
e.printStackTrace();
}
} // end main
}
这里是myprog.java的内容:
import java.io.*;
public class myprog {
public static void main(String args[]) throws IOException {
System.out.println("Hello world!");
System.out.println("Enter something:");
BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
// the readLine() command causes ProcessBuilder to hang
cin.readLine();
}
}
而且input.txt的文件只是
p
此output.txt文件应
Hello world!
Enter something:
你是什么意思的用户输入要求?你能不能展示你的myprog或至少它最相关的部分?另外,如果您想在此获得更好的帮助,请遵循Java命名约定。您使用非标准的命名(包括不使用大写的第一个字母名称)会使您的代码混淆。 – 2012-04-18 14:49:45
我前一段时间回答这个.. http://stackoverflow.com/questions/3062305/executing-shell-commands-from-java/3062874#3062874 – dsmith 2012-04-18 14:51:40
@HovercraftFullOfEels:我添加myprog.java来描述的内容。我很抱歉没有大写类名。 – missthang 2012-04-18 15:44:15