简体   繁体   English

通过Java运行外部程序(Siesta)

[英]Running external program (Siesta) by java

I've checked many threads about running external programs but they cant solve my problem. 我检查了许多有关运行外部程序的线程,但它们无法解决我的问题。 for running Siesta (DFT Calculation) I have to use something like this (Si.fdf is the input file): siesta < Si.fdf I'm using this code: 为了运行Siesta(DFT计算),我必须使用类似以下内容(Si.fdf是输入文件):siesta <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;

} }

but this code only runs Siesta without any input file. 但是此代码仅在没有任何输入文件的情况下运行Siesta。

Finally I've solve this. 终于我解决了。 I add a batch file to terminal that the program control it's contents. 我将一个批处理文件添加到终端,由程序控制其内容。 By running this batch file the problem solved. 通过运行此批处理文件,问题得以解决。

    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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM