0
我已经检查了很多关于运行外部程序的线程,但他们无法解决我的问题。 运行午睡(DFT计算),我不得不使用这样的事情(Si.fdf是输入文件): 午睡< Si.fdf 我使用这个代码:由java运行外部程序(Siesta)
public static void main(String argv[]) throws IOException {
Runtime r = Runtime.getRuntime();
Process p;
BufferedReader is;
String line;
System.out.println("siesta < Si.fdf");
p = r.exec("siesta < Si.fdf");
System.out.println("In Main after exec");
is = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = is.readLine()) != null)
System.out.println(line);
System.out.println("In Main after EOF");
System.out.flush();
try {
p.waitFor();
} catch (InterruptedException e) {
System.err.println(e); //
return;
}
System.err.println("Process done, exit status was " + p.exitValue());
return;
}
但这段代码只运行Siesta,没有任何输入文件。
您正在通过'p.getInputStream()'读取子进程的'stdout'。你需要通过'p.getOutputStream()'将你的数据提供给子进程的'stdin'。将'
我已经使用了 OutputStream out = p.getOutputStream(); out.write(“
user3578884
我的意思是你应该把你的输入文件的**内容**写入你的子进程的'stdin'中。你通过让Bash为你工作解决了你的问题; Bash理解字符“<”作为读取输入文件并将其内容写入Siesta的“stdin”的指令。 Java不像这样解释'<'字符,它不是Unix shell。为了在Java中实现相同的功能,您必须重新创建Bash的功能:读取输入文件的内容,并将其推入子进程的“stdin”中。 –