gpt4 book ai didi

java - 通过java运行外部程序(Siesta)

转载 作者:太空宇宙 更新时间:2023-11-04 12:53:34 25 4
gpt4 key购买 nike

我检查了很多关于运行外部程序的线程,但它们无法解决我的问题。为了运行 Siesta(DFT 计算),我必须使用类似这样的东西(Si.fdf 是输入文件):午睡 < Si.fdf我正在使用这段代码:

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。

最佳答案

我终于解决了这个问题。我向终端添加一个批处理文件,程序控制它的内容。通过运行这个批处理文件,问题就解决了。

    public static void writeBatchFile(String batchFileName, String fileName, String inputFile) throws Exception{
FileWriter write = new FileWriter(batchFileName);
PrintWriter print_line = new PrintWriter(write);
print_line.println("#!/bin/bash");
print_line.println("siesta < "+ inputFile +".fdf"+ " > " + fileName + ".out");
print_line.close();
}

关于java - 通过java运行外部程序(Siesta),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36409377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com