0

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.

amirre
  • 21
  • 5
  • You are reading the `stdout` of the child process via `p.getInputStream()`. You need to feed your data into the `stdin` of the child process via `p.getOutputStream()`. Adding `< Si.fdf` to your `Runtime.exec()` parameter won't do what you want. – wool.in.silver Apr 04 '16 at 17:53
  • I've used the OutputStream out = p.getOutputStream(); out.write("< Si.fdf".getBytes()); out.close(); But the " < Si.fdf" was sent after running Siesta not as a argument for running it. – amirre Apr 04 '16 at 18:02
  • I meant that you should write the **contents** of your input file into the `stdin` of your child process. You solved your problem by having Bash do that work for you; Bash understands the character `<` as an instruction to read the input file and write its contents to `stdin` of Siesta. Java does not interpret the `<` character like that, it is not a Unix shell. To achieve the same thing in Java, you must recreate what Bash does: read the contents of the input file, and push those into the `stdin` of your child process. – wool.in.silver Apr 05 '16 at 09:31
  • Thanks. Finally it worked. I used your advice and used final Process p = Runtime.getRuntime().exec("siesta"); OutputStream out = p.getOutputStream(); out.write(fileContents.getBytes()); out.close(); and completly worked. – amirre Apr 05 '16 at 12:17

1 Answers1

0

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();
}
amirre
  • 21
  • 5