2

I want to execute this command:

/ceplinux_work3/myName/opt/myCompany/ourProduct/bin/EXECUTE_THIS -p cepamd64linux.myCompany.com:19021/ws1/project_name < /ceplinux_work3/myName/stressting/Publisher/uploadable/00000.bin >> /ceplinux_work3/myName/stressting/Publisher/stats/ws1.project_name.19021/2011-07-22T12-45-20_PID-2237/out.up

But it doesn't work because EXECUTE_THIS requires an input file via redirect, and simply passing this command to Runtime.exec doesn't work.

Side note: I searched all over on how to solve this before coming here to ask. There are many questions/articles on the web regarding Runtime.exec and Input/Output redirect. However, I cannot find any that deal with passing a file to a command and outputting the result to another file. Plus, I am totally unfamiliar with Input/Output streams, so I have a hard time putting all the info out there together for my specific situation.

That said, any help is much appreciated.

P.S. If there are multiple ways to do this, I prefer whatever is fastest in terms of throughput.

Edit: As discussed in my last question, I CANNOT change this to a bash call because the program must wait for this process to finish before proceeding.

Ryan
  • 729
  • 1
  • 10
  • 25

1 Answers1

2

Unless you are sending a file name to the standard input of the process, there is no distinction of whether the data came from a file or from any other data source.

You need to write to the OutputStream given by Process.getOutputStream(). The data you write to it you can read in from a file using a FileInputStream.

Putting that together might look something like this:

    Process proc = Runtime.getRuntime().exec("...");

    OutputStream standardInputOfChildProcess = proc.getOutputStream();
    InputStream dataFromFile = new FileInputStream("theFileWithTheData.dat");

    byte[] buff = new byte[1024];
    for ( int count = -1; (count = dataFromFile.read(buff)) != -1; ) {
        standardInputOfChildProcess.write(buff, 0, count);
    }

I've left out a lot of details, this is just to get the gist of it. You'll want to safely close things, might want to consider buffering and you need to worry about the pitfalls of Runtime.exec().

Edit

Writing the output to a file is similar. Obtain a FileOutputStream pointing to the output file and write the data you read from Process.getInputStream() to that OutputStream. The major caveat here is that you must do this operation in a second thread, since accessing two blocking streams from the same thread will lead to deadlock (see the article above).

Mark Peters
  • 80,126
  • 17
  • 159
  • 190
  • I see. However, I want to mimic what would happen to a real call to the program since this is a simulation for a stress test, and I am gathering throughput data on this specific call. Therefore, depending on the buffer size, the results would be skewed, would they not? – Ryan Jul 22 '11 at 21:11
  • Or, I guess, better worded: What buffer size should I use to best mimic a real call? (If this is even an issue.) – Ryan Jul 22 '11 at 21:13
  • @Ryan: it shouldn't make much of a difference. I don't have any idea what buffer size you should use though since I don't know your exact environment or how you pipe in the input. If you're just sending data as fast as the hard drive can give it and the process can take it, the code above will follow it very closely. – Mark Peters Jul 23 '11 at 03:02