Running external program (Siesta) by java

89 views Asked by At

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:

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.

1

There are 1 answers

0
amirre On

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