【发布时间】:2012-04-18 14:42:22
【问题描述】:
我想编写一个运行外部“java myprog output.txt”命令的Java 程序。最终目标是在两个不同的程序上运行此命令,并比较它们与各自输出文件的输出相似性。
我想我已经阅读了几乎所有关于使用 ProcessBuilder 运行外部程序的相关文章,以及关于在该外部程序中处理用户输入的少数条目,但我仍然无法让事情正常工作。根据我的阅读,我认为最好的方法是不运行上面的确切命令,而是读取 input.txt 文件并将其逐字节输入 Process 对象,然后收集输出并将其写入输出.txt ...我对其他选项100%开放。
我根据我的阅读整理了下面的代码。它似乎正确地将 input.txt 中的输入输入到 myprog,但是当我尝试将外部程序的输出打印到控制台以进行验证时,程序挂起在 myprog 中预期(意外)用户输入的位置。
无论是否使用 redirectErrorStream(true) 行,我都会遇到相同的问题。
我真的希望这是用 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 命名约定。您使用非标准命名(包括不将类名的首字母大写)使您的代码混乱。
-
我前段时间回答过这个问题。stackoverflow.com/questions/3062305/…
-
@HovercraftFullOfEels:我在描述中添加了 myprog.java 的内容。对于没有将班级名称大写,我深表歉意。
-
@dsmith:我使用命令“java myprog”和“java myprog
-
您可能需要同时处理标准流。我会给气垫船代表。
标签: java user-input processbuilder external-process