0

I am trying to create a function within a Java Class that can execute a .exe or a .linux file during runtime. The program is espresso.exe (for Windows OS) and espresso.linux for (Linux based systems)

Typically, the way to run the program is by going to command line, and going to the folder in which the executable is stored and typing:

(in Command Prompt)

espresso A0.txt > m.txt

or espresso A0.txt (which returns the output in cmd)

(in linux Terminal)

./espresso.linux A0.txt > m.txt

or ./espresso.linux A0.txt (which returns the output in the terminal window)

Here A0.txt is the input argument and m.txt is the file that espresso creates.

I have stored A0.txt and espresso.linux and espresso.exe under a folder src/resources

I tried the following:

 ProcessBuilder pb = new ProcessBuilder("./src/resources/espresso.exe","src/resources/A0.txt",">src/resources/m.txt");
 try {
        Process p = pb.start();
 }catch (IOException ex) {
        Logger.getLogger(NetSynth.class.getName()).log(Level.SEVERE, null, ex);
 }

I also tried:

Runtime rt = Runtime.getRuntime();
Process p = rt.exec("src/resources/espresso.linux src/resources/A0.txt > src/resources/m.txt");
int waitFor = p.waitFor();

Both of them fail to identify the file to be executed and do not run the command. I understand there may be many errors in the 2 approaches. I could use some help to figure out the approach and the code to be written to run the executable file.

Also, is there a path to be mentioned to run espresso.linux? Will /src/resources/espresso.linux suffice?

Thanks in advance.

2 Answers2

2

You can't do standard output redirection like this (because the ">" sign is interpreted by the OS shell), see this answer for a working solution: ProcessBuilder redirecting output

Since Java 7 there is a Java-only solution in order to achieve redirecting: http://tamanmohamed.blogspot.co.at/2012/06/jdk7-processbuilder-and-how-redirecting.html

Community
  • 1
  • 1
lbalazscs
  • 17,474
  • 7
  • 42
  • 50
  • These files are stored in src/resources in the Java project's Source Packages. Is there a way to capture the output stream. Also is the path I have provided in the exec() or processbuilder() function correct? – Prashant Vaidyanathan Nov 01 '13 at 22:13
2

The > is a shell syntax. If you want to redirect the output to a file you need to use a shell or read the output and write it to a file yourself.

The way you have used > it is just another argument.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130