processoutput4j

Simple Java library for capturing stdout/stderr of processes launched from within Java.

License

License

GroupId

GroupId

com.github.fracpete
ArtifactId

ArtifactId

processoutput4j
Last Version

Last Version

0.0.11
Release Date

Release Date

Type

Type

jar
Description

Description

processoutput4j
Simple Java library for capturing stdout/stderr of processes launched from within Java.
Project Organization

Project Organization

University of Waikato, Hamilton, NZ
Source Code Management

Source Code Management

https://github.com/fracpete/processoutput4j

Download processoutput4j

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.fracpete/processoutput4j/ -->
<dependency>
    <groupId>com.github.fracpete</groupId>
    <artifactId>processoutput4j</artifactId>
    <version>0.0.11</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.fracpete/processoutput4j/
implementation 'com.github.fracpete:processoutput4j:0.0.11'
// https://jarcasting.com/artifacts/com.github.fracpete/processoutput4j/
implementation ("com.github.fracpete:processoutput4j:0.0.11")
'com.github.fracpete:processoutput4j:jar:0.0.11'
<dependency org="com.github.fracpete" name="processoutput4j" rev="0.0.11">
  <artifact name="processoutput4j" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.fracpete', module='processoutput4j', version='0.0.11')
)
libraryDependencies += "com.github.fracpete" % "processoutput4j" % "0.0.11"
[com.github.fracpete/processoutput4j "0.0.11"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

processoutput4j

Simple Java library for capturing stdout/stderr of processes launched from within Java.

Maven

Add the following artifact to your dependencies of your pom.xml:

    <dependency>
      <groupId>com.github.fracpete</groupId>
      <artifactId>processoutput4j</artifactId>
      <version>0.0.11</version>
    </dependency>

Available schemes

The following schemes for capturing process output are available:

  • CollectingProcessOutput - collects all the output and makes it available once the process has finished
  • ConsoleOutputProcessOutput - simply outputs the process' output from stdout and stderr to the Java process' stdout and stderr as it occurrs rather than waiting till the process finishes.
  • StreamingProcessOutput - requires an owner object that implements the StreamingProcessOwner interface, as it will receive the output collected from stdout/stderr for further processing in the owner.

Stopping

The AbstractProcessOutput class offers the destroy() method, which allows a currently running process to be killed off from another thread (the monitor methods are waiting for the process to finish).

Extending

Adding a new scheme for capturing the process output is quite simple. You basically need to implement two classes:

  • a reader class, derived from AbstractProcessReader that does something with the data obtained from stdout and stderr of the process.
  • an output class, derived from AbstractProcessOutput that instantiates the appropriate readers for stdout and stderr.

Examples

The following executes the process and outputs any data from stdout/stderr once the process has finished:

import com.github.fracpete.processoutput4j.output.ConsoleOutputProcessOutput;
...
String[] cmd = new String[]{"/bin/ls", "-la", "/some/where"};
ProcessBuilder builder = new ProcessBuilder();
builder.command(cmd);
new ConsoleOutputProcessOutput().monitor(builder);

The following executes the process and outputs any data from stdout/stderr once the process has finished:

import com.github.fracpete.processoutput4j.output.CollectingProcessOutput;
...
String[] cmd = new String[]{"/bin/ls", "-la", "/some/where"};
ProcessBuilder builder = new ProcessBuilder();
builder.command(cmd);
CollectingProcessOutput output = new CollectingProcessOutput();
output.monitor(builder);
System.out.println("exit code: " + output.getExitCode());
System.out.println(output.getStdOut());

The following executes the process and outputs any data from stdout/stderr as it occurs, as well as setting a maximum execution time of 10 seconds:

import com.github.fracpete.processoutput4j.output.CollectingProcessOutput;
...
String[] cmd = new String[]{"/usr/bin/find", "/"};
ProcessBuilder builder = new ProcessBuilder();
builder.command(cmd);
ConsoleOutputProcessOutput output = new ConsoleOutputProcessOutput();
output.setTimeOut(10);
output.monitor(builder);

You can use the hasTimedOut() method to check whether the process timed out.

If you want to process the output (stdout/stderr) from the process yourself, then you can use StreamingProcessOutput instead of ConsoleOutputProcessOutput. You only need to supply an object of a class implementing the StreamingProcessOwner interface. Below is an example that simply prefixes the output with either [OUT] or [ERR]:

import com.github.fracpete.processoutput4j.core.StreamingProcessOutputType;
import com.github.fracpete.processoutput4j.core.StreamingProcessOwner;
import com.github.fracpete.processoutput4j.output.StreamingProcessOutput;

public static class Output implements StreamingProcessOwner {
  public StreamingProcessOutputType getOutputType() {
    return StreamingProcessOutputType.BOTH;
  }
  public void processOutput(String line, boolean stdout) {
    System.out.println((stdout ? "[OUT] " : "[ERR] ") + line);
  }
}

...
String[] cmd = new String[]{"/usr/bin/find", "/etc"};
ProcessBuilder builder = new ProcessBuilder();
builder.command(cmd);
StreamingProcessOutput output = new StreamingProcessOutput(new Output());
output.monitor(builder);

Instead of implementing StreamingProcessOwner yourself, you can use one of the following implementations:

  • com.github.fracpete.processoutput4j.core.impl.PrefixedStreamingProcessOwner

    Forwards the stdout/stderr output of the monitored process to this one's stdout, using either default prefixes for stdout/stderr or user-supplied ones (like the above example).

  • com.github.fracpete.processoutput4j.core.impl.SimpleStreamingProcessOwner

    Forwards the stdout/stderr output of the monitored process to this one's stdout/stderr.

Versions

Version
0.0.11
0.0.10
0.0.9
0.0.8
0.0.7
0.0.6
0.0.5
0.0.4
0.0.3
0.0.2
0.0.1