0

I am using Windows!

I want to call a small .exe application from my java command line which is called "saucy.exe". It needs an input file "input.saucy". Both are stored in the correct directory.

When I use the command

Process p = Runtime.getRuntime().exec("saucy input.saucy");

everything works fine and I get an output on the console.

However, when I try to write the output in a file

Process p = Runtime.getRuntime().exec("saucy input.saucy > output.saucy");

nothing happens.

I already found the advice in http://www.ensta-paristech.fr/~diam/java/online/io/javazine.html and tried to tokenize the command manually:

String[] cmd = {"saucy", "input.saucy > output.saucy"};
Process p = Runtime.getRuntime().exec(cmd);

It is still not working. Any advice? It is no option for me to write the output to a file with java code, because its too slow.

Again: I am using Windows (I stress that because I read several hints for Linux systems).

5
  • 3
    > is a shell command, but you are not using one. try String[] cmd = { "cmd", "/C", "saucy input.saucy > output.saucy" }; Commented Oct 8, 2012 at 17:58
  • Hi, this was the solution! Thank you very much! Commented Oct 9, 2012 at 10:05
  • Added it as a answer so you can accepted by ticking on the left hand side. ;) Commented Oct 9, 2012 at 10:06
  • If you now yould give me an advice how i can execute the program, if it is in a folder (e.g. "folder"). I tried String[] cmd = { "cmd", "/C", "folder/saucy input.saucy > output.saucy" }; and String[] cmd = { "cmd", "/C", "folder\\saucy input.saucy > output.saucy" }; which gave me the error that: application 'folder' could not been found. Commented Oct 9, 2012 at 10:09
  • When you give a relative path, it is relative to the current working directory. If you don't know what that will be you need to give a full path like C:\\basedirectory\\folder\\saucy Commented Oct 9, 2012 at 10:11

3 Answers 3

1

> is a shell command, but you are not using one. try

String[] cmd = { "cmd", "/C", "saucy input.saucy > output.saucy" }; 
Sign up to request clarification or add additional context in comments.

Comments

1

If you are on Java 7 you can use the new ProcessBuilder.redirectOutput mechanism:

ProcessBuilder pb = new ProcessBuilder("saucy", "input.saucy");

// send standard output to a file
pb.redirectOutput(new File("output.saucy"));
// merge standard error with standard output
pb.redirectErrorStream(true);

Process p = pb.start();

Comments

0

Use the getInputStream(), getOutputStream() and getErrorStream() to retrieve the output (or send input).

http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Process.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.