Tiny, pure object-oriented, declarative and immutable wrapper of zt-exec with additional features around a process execution. Inspired by approach used in the Cactoos lib.
Java 8+ required.
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.
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
- Get the latest version here with or without dependencies
- 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.29</version>
</dependency>
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
.interpreter("sh")
.command("-c", "s='Hello'; echo $s;").execute()
.outputUTF8();
System.out.println(output);
// output> Hello
Save output stream into a file, even if the process stopped unexpectedly
new Cmd()
.outputFileName("output.txt")
.command("echo", "Hello").execute();
Execute command inside a separate work directory. It creates work directory before start and delete after finish
new Cmd()
.configuring(c -> c.directory(new File("./", "foo"))) // specify work directory ./foo
.cleanUp(true) // delete work directory after process stopped, only if the directory will be created during the execution
.listening().afterStop(process -> {
//work directory ./foo will be exist here
}).back()
.command("echo", "hello world").execute(); // work directory ./foo will be created automatically
//work directory ./foo was deleted after execution
Run command in a background
StartedProcess startedProcess = new Cmd().command("echo", "Hello").start();
startedProcess.getFuture().get(); //wait result