/CmdTool

Execute console commands and scripts from Java easily

Primary LanguageJavaEclipse Public License 1.0EPL-1.0

CmdTool

Continuous Integration

Build Status codecov Maven Central

Quick Overview

Tiny, pure object-oriented, declarative and immutable wrapper of zt-exec with additional features around a process execution.

Java 8+ required.

Motivation

When we call external programs from Java, we certainly need to harvest the output files and output stream. It is ok, but what if we have thousands of calls? They will pollute a disk space if some of them produce files we don't need. So, we have to do a clean up of disk space if the files don't need anymore, just like Java GC frees a RAM automatically.

To be honest, initial motivation was selfish. I wanted to practice the design principles behind Cactoos. And as a result, it grew into the tiny project here.

Features

All features of zt-exec are supported plus the following:

  • Execute command or script
  • Save output stream of the process into a file
  • Create and clean up work directory automatically

Download

Include the dependency into your pom.xml

<dependency>
  <groupId>io.github.alekseysotnikov</groupId>
  <artifactId>CmdTool</artifactId>
  <version>1.0.1</version>
</dependency>

Or

  1. Get the latest version here with or without dependencies
  2. Project uses the following dependencies
<dependency>
  <groupId>org.zeroturnaround</groupId>
  <artifactId>zt-exec</artifactId>
  <version>1.10</version>
</dependency>

<dependency>
  <groupId>org.cactoos</groupId>
  <artifactId>cactoos</artifactId>
  <version>0.30</version>
</dependency>

Examples

Execute command

new Cmd().command("echo", "Hello").execute();

Execute script in a Shell (Unix-like, Mac OS)

new Cmd()
       .interpreter("sh") // specify command interpreter
       .command("-c", "s='Hello'; echo $s;")
       .execute();

or even shorter

new Cmd().command("sh", "-c", "s='Hello'; echo $s;").execute();

... and read output

String output = new Cmd()
                     .configuring(c -> c.readOutput(true)) // configure zt-exec's executor
                     .command("sh", "-c", "s='Hello'; echo $s;")
                     .execute()
                     .outputUTF8();
System.out.println(output);

// output> Hello

Save an output stream into a file, even if the process stopped unexpectedly

new Cmd()
      .configuring(
              RedirectToFile.fromOutputStream("./output.txt"),
              RedirectToFile.fromErrorStream("./errOutput.txt"))
      .command("echo", "Hello")
      .execute();

Execute command within custom work directory

new Cmd()
        .configuring(
                new WorkDir("./foo"), // specify work directory ./foo (will be created automatically)
                new CleanUp() // delete work directory after process stop
        ) 
        .listening((Listening.AfterStop) process -> {
            System.out.println(new File("./foo").exists()); //true
        })
        .command("echo", "Hello")
        .execute();

System.out.println(new File("./foo").exists()); // false

Run command in a background

StartedProcess startedProcess = new Cmd().command("echo", "Hello").start();
startedProcess.getFuture().get(); //wait result