1

I am getting some problem while executing binary executable file using Java code.

My Executable file is running perfectly on Terminal(Linux os) using following command

./ab0818 < ab0818.challenge

where "ab0818" is executable file and "ab0818.challenge" is input file.

I will get 0 exit code after running this command in terminal

My Code is Bellow.

System.out.println("Running the batch script");

Process p = Runtime.getRuntime().exec("./ab0818 < ab0818.challenge");
p.waitFor();
System.out.println("is.read() = "+p.exitValue());   

when I am run my code it will never come out of wait(waitFor()) process and my program never terminates.

My question is there any alternative way to execute command using java code or is there any modification needed in my code.

Thanks in Advance, -Viraj

Viraj
  • 590
  • 7
  • 17
  • This looks rather similar to http://stackoverflow.com/questions/5886935/how-do-i-get-the-bash-command-exit-code-from-a-process-run-from-within-java – Isaac Truett May 04 '11 at 18:44

1 Answers1

1

Java doesn't know about the Linux shell's redirection operator "<".

You could try:

...exec("bash -c './ab0818 < ab0818.challenge'")

You might also want to look at http://download.oracle.com/javase/6/docs/api/java/lang/ProcessBuilder.html

Paul Cager
  • 1,910
  • 14
  • 21
  • Thanks this approach works for me. May I what bash -c command do? – Viraj May 04 '11 at 18:42
  • It executes the bash shell, which in turn executes your program. Type "man bash" to see the manual page which describes all the options. – dnault May 04 '11 at 19:28