Skip to content

yegor256/jaxec

Repository files navigation

EO principles respected here DevOps By Rultor.com We recommend IntelliJ IDEA

mvn PDD status Maven Central Javadoc codecov Hits-of-Code Lines of code License

It's a simple executor of a shell command from Java. It is essentially a wrapper around Runtime.exec(), with a fluent interface.

First, you add this to your pom.xml:

<dependency>
  <groupId>com.yegor256</groupId>
  <artifactId>jaxec</artifactId>
  <version>0.3.1</version>
</dependency>

Then, you use it like this:

import com.yegor256.Jaxec;
String stdout = new Jaxec("ls", "-al")
  .with("/tmp") // append argument to the command
  .withHome("/home/me") // run it in this directory
  .withRedirect(false) // don't redirect STDERR to STDOUT
  .withCheck(false) // don't throw if the exit code is not-zero
  .withStdin("Hello, world!") // send this text to the STDIN of the command
  .exec();

If exit code is not equal to zero, a runtime exception will be thrown by the exec() method. You can also use unsafeExec(), which throws checked exception IOException.

The stdout and stderr of the command are both sent to Slf4j logging facility com.jcabi.log.VerboseProcess. The level for stdout is DEBUG, while the level for stderr is WARN.

How to Contribute

Fork repository, make changes, send us a pull request. We will review your changes and apply them to the master branch shortly, provided they don't violate our quality standards. To avoid frustration, before sending us your pull request please run full Maven build:

$ mvn clean install -Pqulice

You will need Maven 3.3+ and Java 8+.

About

Primitive execution of command line commands from Java (mostly useful for tests)

Topics

Resources

License

Stars

Watchers

Forks

Languages