我想编写一个运行外部"java myprog < input.txt > output.txt“命令的Java程序。最终目标是在两个不同的程序上运行此命令,并从各自的输出文件中比较它们的输出相似性。
我想我已经阅读了几乎所有关于使用ProcessBuilder运行外部程序的相关文章,以及关于在该外部程序中处理用户输入的几篇文章,但我仍然不能让事情正常工作。根据我所读到的,我认为最好的方法是不运行上面的命令,而是读取input.txt文件并将其逐个字节地提供给Process对象,然后收集输出并将其写入output.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:
发布于 2012-04-18 16:39:40
我想知道你的问题是否部分是因为没有使用单独的线程来读取输入和写入输出。例如:
public static void main(String args[]) {
try {
// WANT: "java myprog < input.txt > output.txt"
String inputFile = "input.txt";
String outputFile = "output.txt";
// my ProcessBuilder Strings will be different from yours
ProcessBuilder pb = new ProcessBuilder("java", "-cp", ".;bin;",
"yr12.m04.a.MyProg");
pb.redirectErrorStream(true);
Process p = pb.start();
final OutputStream pos = p.getOutputStream();
final PrintWriter pw = new PrintWriter(pos);
final InputStream fis = new FileInputStream(inputFile);
final BufferedReader fileBr = new BufferedReader(new InputStreamReader(fis));
InputStreamReader isr = new InputStreamReader(p.getInputStream());
final BufferedReader br = new BufferedReader(isr);
new Thread(new Runnable() {
public void run() {
String lineRead;
try {
while ((lineRead = br.readLine()) != null) {
System.out.println(lineRead);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}).start();
new Thread(new Runnable() {
public void run() {
try {
String lineRead;
while ((lineRead = fileBr.readLine()) != null) {
pw.println(lineRead);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (pw != null) {
pw.close();
}
if (fileBr != null) {
try {
fileBr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}).start();
} catch (IOException e) {
e.printStackTrace();
}
} // end main
发布于 2012-04-18 14:50:19
你有没有想过改用Runtime.getRuntime().exec()?
Process proc = Runtime.getRuntime().exec("java myprog "+inputFile+" "+outputFile);
发布于 2012-04-18 16:07:07
您可以包含'myprog‘的jar文件并自己调用main()方法。更重要的是,如果myprog在您的域中,您可以完全摆脱main方法。
https://stackoverflow.com/questions/10211821
复制相似问题