View on GitHub


A Java library which enables you to easily execute arbitrary OS command in a subprocess, and to get the command result: return code as int, STDOUT and STDERR as List of String.

You can execute arbitrary OS command using com.kazurayam.subprocessj.Subprocess in your Java application.

The artifacts of subprocessj are available at the Maven Central repository:


Javadoc is here


You just call<String> command). The run() will wait for the sub-process to finish, and returns a com.kazurayam.subprocessj.CompletedProcess object which contains the return code, STDOUT and STDERR emitted by the sub-process.

package com.kazurayam.subprocessj;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import static org.junit.jupiter.api.Assertions.*;

class SubprocessTest {

    void test_demo() throws Exception {
        Subprocess subprocess = new Subprocess();
        subprocess.cwd(new File(System.getProperty("user.home")));
        CompletedProcess cp ="ls", "-la", "."));

This will emit the following output in the console:

total 4712
drwxr-xr-x+  90 kazurayam       staff     2880  7 31 21:01 .
drwxr-xr-x    6 root            admin      192  1  1  2020 ..

Motivation, etc.

There are many articles that tell how to use java.lang.ProcessBuilder. For example, I learned “Baeldung article: Run Shell Command in Java”. The ProcessBuilder class is a state of the art with rich set of functionalities. But for me it is not very easy to write a program that utilized ProcessBuilder. It involves multi-threading to consume the output streams (STDOUT and STDERR) from subprocess. I do not want to repeat writing it.

So I have made a simple wrapper of ProcessBuilder which exposes a limited subset of its functionalities.

I named this as subprocjessj as I meant it to be a homage to the Subprocess module of Python.