Java exec: How to execute a system command pipeline in Java

In earlier articles I've described how to execute system commands from Java applications. A long time ago I wrote my first article on this topic (How to execute system commands from Java), and more recently I wrote an updated version of that article titled "Executing system commands from Java using the ProcessBuilder and Process classes".

Given that introduction -- if you're interested in learning how to execute a Unix or Linux system pipeline (pipe) command from a Java application, you're in the right place.

How to exec a system pipeline (pipe) command from Java

Jumping right in, let's imagine that you want to run the following Unix/Linux command from your Java application:

ls -l /var/tmp | grep foo

The following source code shows how to execute that command from a Java application:

package com.devdaily.system;

import java.util.*;

public class ProcessBuilderExample
  public static void main(String[] args) 
  throws IOException, InterruptedException
    // you need a shell to execute a command pipeline
    List<String> commands = new ArrayList<String>();
    commands.add("ls -l /var/tmp | grep foo");

    SystemCommandExecutor commandExecutor = new SystemCommandExecutor(commands);
    int result = commandExecutor.executeCommand();

    StringBuilder stdout = commandExecutor.getStandardOutputFromCommand();
    StringBuilder stderr = commandExecutor.getStandardErrorFromCommand();



That code shows you both (a) how to construct a command pipeline, (b) how to execute the command, and (c) how to get the output from that command, all using a class I wrote named SystemCommandExecutor.

I'm not going to describe the SystemCommandExecutor class in this article; it's actually fairly complicated, and I described it in my "Executing system commands from Java using the ProcessBuilder and Process classes" tutorial. The only thing I'll say about it here is that you can download the SystemCommandExecutor and its supporting classes from that article.

Java exec pipe - you need a shell

The only thing I'd like to add here today is that the most important part of this solution was realizing that when you exec a system command from a Java application, you don't actually get a Unix or Linux shell to run your command in. You're really just running the command without a shell wrapper. So, to use a feature like a Unix/Linux pipe (pipeline) -- which is a shell feature -- you have to invoke a shell, and then run your commands inside that shell.

That's what I'm doing in the lines of code above, invoking a shell (/bin/sh), and then running the "ls -l /var/tmp | grep foo" command pipeline in that shell.

Java execute system command pipeline - summary

I hope this tutorial on how to execute a system command pipeline (pipe) from Java has been helpful. I have tested this code on Linux and Mac OS X systems, and it seems to work fine on those two Unix systems. If you have any questions or comments please leave a note in the Comments section below.



The class SystemCommandExecutor is not found. I tried searching for it over and over, but could not locate it.

You can find it in the "Download" section near the bottom of this page:

(Sorry, it's a little hidden at the end of that three-page article.)

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.