我要从java编译并运行c
程序。当运行c程序时,我可以得到错误和c程序的输出文本。但是,如果c程序期望用户输入执行其程序,我如何从java代码提供该用户输入?这就是你所做的。
public void runFile() {
appendToPane(pane, "----------- Program is executing ------------ \n", Color.LIGHT_GRAY);
Process p;
try {
p = Runtime.getRuntime().exec(_TEMP_LOCATION + _FILE_NAME.split(".c")[0]);//exe file exist inside project root folder
BufferedReader reader1 = new BufferedReader(new InputStreamReader(p.getErrorStream()));
BufferedReader reader2 = new BufferedReader(new InputStreamReader(p.getInputStream()));
//OutputStream a = p.getOutputStream();
String line;
while ((line = reader1.readLine()) != null) {
appendToPane(pane, line + "\n", null);
}
while ((line = reader2.readLine()) != null) {
appendToPane(pane, line + "\n", null);
}
//p.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
appendToPane(pane, "----------- executing is done ------------ \n", Color.LIGHT_GRAY);
}
答案 0 :(得分:2)
进程还为您提供了一个OutputStream,您可以向其编写内容,并将其作为输入传递给您的进程。请参阅http://docs.oracle.com/javase/7/docs/api/java/lang/Process.html#getOutputStream()。