Take the 2-minute tour ×

I am working with ImageMagick and am wondering what the best way to read image data in from StdOut of the ImageMagick process.

I an invoking the command to the process like so: /opt/local/bin/convert -resize 8000@ - -

and then piping a byte array to the process for processing.

what is the best way to read data from a process like this?

share|improve this question

4 Answers 4

up vote 2 down vote accepted

You might want to look at Apache Commons Exec this gives you a good way of running executables and passing the results into Java.

It's similar to using the method Michael mentioned except should allow you more control.

There are some good examples of usage on the tutorial page.

share|improve this answer
Thanks, my problems were mostly between the chair and keyboard and due to hacking way too late in the night. However, your tip for Apache Exec paid off and improved the quality of the solution. –  Daniel Honig Mar 7 '09 at 21:42

Just want to post a code sample using ImageMagick via Apache commons exec for completeness

    try {

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < args.size(); i++) {
            String s = args.get(i);

        CommandLine cl = CommandLine.parse(sb.toString());

        PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(output, System.err, new ByteArrayInputStream(inputBytes));
        byAs = new ByteArrayInputStream(inputBytes);

        DefaultExecutor executor = new DefaultExecutor();
        int exitValue = executor.execute(cl);

        outputBytes = output.toByteArray();
    } catch (IOException e) {
    } finally {
        try {
            if (byAs != null)

            if (output != null)

        } catch (IOException e) {

share|improve this answer

The Runtime.exec() method returns a Process that has methods for getting the input, output and error streams of the invoked process.

I do wonder how you managed to "pipe a byte array to the process" in any other way.

Note that you'll have to use multiple threads to handle the input and the output of the spawned process because it will block if you try to "pipe in" a lot of input without reading any output.

share|improve this answer

If you do want to use Runtime.exec(), please review this question as runtime.exec() can be a bit tricky.

You will be wanting to change the StreamGobbler for OUTPUT into something that stores stdout in an array or some such. And start the threads before doing waitFor() to prevent, as Michael said, blocking.

share|improve this answer

Your Answer


