• Stars
    star
    876
  • Rank 52,107 (Top 2 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created over 11 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

ZeroTurnaround Process Executor

ZT Process Executor

Continuous Integration

Build Status

Quick Overview

The project was created to merge similar functionality of projects at ZeroTurnaround into a single codebase. It's designed to be powerful but still simple to use. By using a single class ProcessExecutor the user gets the functionality from both java.lang.ProcessBuilder and Apache Commons Exec.

Dependencies

  • Minimal required Java version is 6 (tested also with 8, 11 and 17).
  • The only 3rd-party dependency is SLF4J.

Installation

Maven Central

The project artifacts are available in Maven Central Repository.

To include it in your maven project then you have to specify the dependency.

...
<dependency>
    <groupId>org.zeroturnaround</groupId>
    <artifactId>zt-exec</artifactId>
    <version>1.12</version>
</dependency>
...

Motivation

There are many approaches to take when running external processes from Java. There are the JRE options such as the Runtime.exec() and ProcessBuilder. Also there is the Apache Commons Exec. Nevertheless we created yet another process library (YAPL).

Some of the reasons for this crazy endeavour

  • Improved handling of streams
  • Reading/writing to streams
  • Redirecting stderr to stdout
  • Improved handling of timeouts
  • Improved checking of exit codes
  • Improved API
  • One liners for quite complex usecases
  • One liners to get process output into a String
  • Access to the Process object available
  • Support for async processes ( Future )
  • Improved logging with SLF4J API
  • Support for multiple processes

Can zt-exec also kill processes?

No. There is zt-process-killer for that.

Examples

  • Output is pumped to NullOutputStream
new ProcessExecutor().command("java", "-version").execute();

  • Returning the exit code
  • Output is pumped to NullOutputStream
int exit = new ProcessExecutor().command("java", "-version")
                  .execute().getExitValue();

  • Return output as UTF8 String
String output = new ProcessExecutor().command("java", "-version")
                  .readOutput(true).execute()
                  .outputUTF8();    

  • Pumping the output to a logger
new ProcessExecutor().command("java", "-version")
      .redirectOutput(Slf4jStream.of(LoggerFactory.getLogger(getClass().getName() + ".MyProcess")).asInfo()).execute();

  • Pumping the output to a logger (short form for previous)
new ProcessExecutor().command("java", "-version")
      .redirectOutput(Slf4jStream.of("MyProcess").asInfo()).execute();

  • Pumping the output to the logger of the caller class
new ProcessExecutor().command("java", "-version")
      .redirectOutput(Slf4jStream.ofCaller().asInfo()).execute();

  • Pumping the output to a logger
  • Returning output as UTF8 String
String output = new ProcessExecutor().command("java", "-version")
        .redirectOutput(Slf4jStream.of(getClass()).asInfo())
        .readOutput(true).execute().outputUTF8();

  • Pumping the stderr to a logger
  • Returning the output as UTF8 String
String output = new ProcessExecutor().command("java", "-version")
        .redirectError(Slf4jStream.of(getClass()).asInfo())
        .readOutput(true).execute()
        .outputUTF8();

  • Running with a timeout of 60 seconds
  • Output pumped to NullOutputStream
try {
  new ProcessExecutor().command("java", "-version")
        .timeout(60, TimeUnit.SECONDS).execute();
}
catch (TimeoutException e) {
  // process is automatically destroyed
}

  • Pumping output to another OutputStream
OutputStream out = ...;
new ProcessExecutor().command("java", "-version")
      .redirectOutput(out).execute();

  • Handling output line-by-line while process is running (Java 8+)
new ProcessExecutor().command("java", "-version")
    .redirectOutput(line -> ...)
    .execute();

  • Handling output line-by-line while process is running (prior to Java 8)
new ProcessExecutor().command("java", "-version")
    .redirectOutput(new LogOutputStream() {
      @Override
      protected void processLine(String line) {
        ...
      }
    })
    .execute();

  • Destroy the running process when VM exits
  • Output pumped to NullOutputStream
new ProcessExecutor().command("java", "-version").destroyOnExit().execute();

  • Run process with a specific environment variable
  • Output pumped to NullOutputStream
new ProcessExecutor().command("java", "-version")
    .environment("foo", "bar").execute();

  • Run process with a specific environment
  • Output pumped to NullOutputStream
Map<String, String> env = ...
new ProcessExecutor().command("java", "-version")
    .environment(env).execute();

  • Throw exception when wrong exit code
  • Output is pumped to NullOutputStream
try {
  new ProcessExecutor().command("java", "-version")
        .exitValues(3).execute();
}
catch (InvalidExitValueException e) {
  System.out.println("Process exited with " + e.getExitValue());
}

  • Throw exception when wrong exit code
  • Return output as UTF8 String
String output;
boolean success = false;
try {
  output = new ProcessExecutor().command("java", "-version")
                .readOutput(true).exitValues(3)
                .execute().outputUTF8();
  success = true;
}
catch (InvalidExitValueException e) {
  System.out.println("Process exited with " + e.getExitValue());
  output = e.getResult().outputUTF8();
}

  • Starting process in the background
  • Output is pumped to NullOutputStream
Future<ProcessResult> future = new ProcessExecutor()
                                    .command("java", "-version")
                                    .start().getFuture();
// do some stuff
future.get(60, TimeUnit.SECONDS);

  • Start process in the background
  • Return output as UTF8 String
Future<ProcessResult> future = new ProcessExecutor()
                                    .command("java", "-version")
                                    .readOutput(true)
                                    .start().getFuture();
// do some stuff
String output = future.get(60, TimeUnit.SECONDS).outputUTF8();

More Repositories

1

zt-zip

ZeroTurnaround ZIP Library
Java
1,373
star
2

sql-formatter

A whitespace formatter for different query languages
TypeScript
1,114
star
3

zt-process-killer

ZeroTurnaround Process Killer
Java
126
star
4

jvm-languages-report

Combined repository for the JVM languages report by RebelLabs
Java
63
star
5

callspy

A simple tracing agent
Java
63
star
6

gradle-jrebel-plugin

The plugin generates rebel.xml configuration file for the Gradle-based project
Java
51
star
7

maven-jrebel-plugin

Generates rebel.xml configuration file for the maven project
Java
51
star
8

stardate-converter

Converts common date to Star Trek stardate
JavaScript
20
star
9

xrebel-docker-demo

Demo project for showcasing XRebel features with distributed apps
Shell
9
star
10

jenkins-reporter

Generate nice reports of your Jenkins views.
Java
7
star
11

giantrobots

7
star
12

isJRebel

Check if a String is equal to JRebel
Java
7
star
13

bad-classes

Utility to debug UnsupportedClassVersionError in Java
Java
6
star
14

zt-hock

JavaScript
4
star
15

strict-spies

A strict alternative for Jasmine spies
JavaScript
4
star
16

atg-hello-demo

Simple demo application for Oracle ATG Web Commerce
Java
4
star
17

build-flow-test-aggregator

Java
4
star
18

jenkins-liverebel-plugin

Jenkins LiveRebel Plugin
Java
3
star
19

cluster-stats

Jenkins Cluster Statistics Plugin
Java
2
star
20

jrebel-tdd

Java
2
star
21

xrebel-demo-supplements

Application included into https://github.com/zeroturnaround/xrebel-docker-demo
Java
2
star
22

eslint-config-zt

ESLint shareable config for ZeroTurnaround JavaScript projects
JavaScript
1
star
23

xr-demo-answers

JavaScript
1
star
24

lunch-bot

JavaScript
1
star
25

rebellabs-report-2017-analysis-app

An analysis app for the data received for the RebelLabs Developer Productivity Report 2017
Java
1
star
26

zt-react-components

Commonly used React components for ZeroTurnaround products
JavaScript
1
star
27

aws-ecs-deploy-jenkins-plugin

Java
1
star
28

netbeans-jrebel-open-plugin

JRebel installer plugin for NetBeans
Java
1
star