5

I'm trying to running an external program from a Java program and I'm having trouble. Basically what I'd like to do would be this:

 Runtime.getRuntime().exec("./extprogram <fileIn >fileOut");

However I've found that that doesn't work - Java apparentls needs to use a Process with input and output streams and other things which I'm not experienced with.

I've looked at a number of examples across the internet (many of which are from SO), and there doesn't seem to be a simple standard way of doing this, which for someone who doesn't fully understand what's going on, can be quite frustrating.

I'm also having trouble trying to build my own code off the examples of other people's code because generally it seems most other people 1. aren't interested in redirecting stdin, and 2. aren't necessarily redirecting stdout to a file, but instead to System.out.

So, would anyone be able to point me in the direction of any good simple code templates for calling external programs and redirecting stdin and stdout? Thanks.

MattS
  • 717
  • 2
  • 7
  • 22

3 Answers3

9

You could try something like this:

ProcessBuilder pb = new ProcessBuilder();
pb.redirectInput(new FileInputStream(new File(infile));
pb.redirectOutput(new FileOutputStream(new File(outfile));
pb.command(cmd);
pb.start().waitFor();
corsiKa
  • 81,495
  • 25
  • 153
  • 204
  • 2
    1+ but don't forget the error stream wich can be folded into the process's InputStream by calling `bp.redirectErrorStream(true)`. – Hovercraft Full Of Eels Jul 04 '12 at 23:12
  • `ProcessBuilder` doesn't have either a `redirectInput()` or a `redirectOutput()` function. http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ProcessBuilder.html – MattS Jul 04 '12 at 23:12
  • @MattS \s\1.5.0\7 http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html – corsiKa Jul 04 '12 at 23:18
  • Ah, I'm only on Java 6. Unfortunately upgrading is not an option at the moment. – MattS Jul 04 '12 at 23:21
  • That's unfortunate. It's been out for almost a year now, and is quite battle tested. :-( hopefully an 'old school' way presents itself! – corsiKa Jul 04 '12 at 23:22
  • I'll talk to the SysAdmins at work, maybe they'll let me upgrade. – MattS Jul 04 '12 at 23:26
  • I don't think that works, there's no method `ProcessBuilder.redirectOutput()` that takes an `OutputStream`. – Nico Aug 06 '15 at 07:15
7

If you must use Process, then something like this should work:

public static void pipeStream(InputStream input, OutputStream output)
   throws IOException
{
   byte buffer[] = new byte[1024];
   int numRead = 0;

   do
   {
      numRead = input.read(buffer);
      output.write(buffer, 0, numRead);
   } while (input.available() > 0);

   output.flush();
}

public static void main(String[] argv)
{
   FileInputStream fileIn = null;
   FileOutputStream fileOut = null;

   OutputStream procIn = null;
   InputStream procOut = null;

   try
   {
      fileIn = new FileInputStream("test.txt");
      fileOut = new FileOutputStream("testOut.txt");

      Process process = Runtime.getRuntime().exec ("/bin/cat");
      procIn = process.getOutputStream();
      procOut = process.getInputStream();

      pipeStream(fileIn, procIn);
      pipeStream(procOut, fileOut);
   }
   catch (IOException ioe)
   {
      System.out.println(ioe);
   }
}

Note:

  • Be sure to close the streams
  • Change this to use buffered streams, I think the raw Input/OutputStreams implementation may copy a byte at a time.
  • The handling of the process will probably change depending on your specific process: cat is the simplest example with piped I/O.
pb2q
  • 58,613
  • 19
  • 146
  • 147
  • This is working for me! Thank you so much. There's just one last problem I'm having with this code - when the program is done the files are empty (they DID have data in them already - it was being used by the program). Why would that be? – MattS Jul 05 '12 at 02:22
  • MattS: I updated the code a bit, the earlier stream handling had a couple of problems (I hadn't considered about the stream handling enough). These changes may help. Also added a few notes. – pb2q Jul 05 '12 at 21:48
  • @pb1q Great, I'll take a look at it in a bit. With the code as it was before, I did remember to close the input/output streams, but I forgot to close the process's error stream, which was 'causing a crash due to too many files open at once. Thanks again. – MattS Jul 05 '12 at 21:51
0

have you tried System.setIn and System.setOut? has been around since JDK 1.0.

public class MyClass
{
    System.setIn( new FileInputStream( "fileIn.txt" ) );
    int oneByte = (char) System.in.read();
    ...

    System.setOut( new FileOutputStream( "fileOut.txt" ) );
    ...
Snake
  • 785
  • 1
  • 5
  • 13