All Projects → vorburger → ch.vorburger.exec

vorburger / ch.vorburger.exec

Licence: Apache-2.0 license
Java library to launch external processes

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to ch.vorburger.exec

Execa
Process execution for humans
Stars: ✭ 4,318 (+16507.69%)
Mutual labels:  execute, exec
Bforartists
Bforartists is a fork of the popular 3D software Blender, with the goal to improve the UI.
Stars: ✭ 240 (+823.08%)
Mutual labels:  fork
Repo Lockdown
GitHub Action that immediately closes and locks issues and pull requests
Stars: ✭ 56 (+115.38%)
Mutual labels:  fork
Github Sync
⤵️ A GitHub Action for syncing current repository with remote
Stars: ✭ 166 (+538.46%)
Mutual labels:  fork
Logisim
Logisim Italian Fork
Stars: ✭ 61 (+134.62%)
Mutual labels:  fork
Kotatogram Desktop
Experimental Telegram Desktop fork.
Stars: ✭ 200 (+669.23%)
Mutual labels:  fork
Lab
Lab wraps Git or Hub, making it simple to clone, fork, and interact with repositories on GitLab
Stars: ✭ 911 (+3403.85%)
Mutual labels:  fork
AROS
www.axrt.org
Stars: ✭ 33 (+26.92%)
Mutual labels:  exec
Pull
🤖 Keep your forks up-to-date via automated PRs
Stars: ✭ 3,364 (+12838.46%)
Mutual labels:  fork
Alpine Term
Repository has been moved.
Stars: ✭ 121 (+365.38%)
Mutual labels:  fork
Fork Sync
🔄 Github action to sync your forks
Stars: ✭ 99 (+280.77%)
Mutual labels:  fork
Branchy
🍃 Execute a Node.js function in a separate process
Stars: ✭ 84 (+223.08%)
Mutual labels:  fork
Enet
⚡️ ENet reliable UDP networking library
Stars: ✭ 202 (+676.92%)
Mutual labels:  fork
Forkcms
Fork is an easy to use open source CMS using Symfony Components.
Stars: ✭ 1,112 (+4176.92%)
Mutual labels:  fork
go-ipfs
Ungx-ed fork of go-ipfs
Stars: ✭ 31 (+19.23%)
Mutual labels:  fork
Errand Boy
A memory-conscious alternative to os.fork() and subprocess.Popen().
Stars: ✭ 34 (+30.77%)
Mutual labels:  fork
Bitcore
BitCore (BTX) - Cryptocurrency 220 Byte Datacarriersize
Stars: ✭ 94 (+261.54%)
Mutual labels:  fork
Beefun Pro
Github client for iOS in Swift.
Stars: ✭ 172 (+561.54%)
Mutual labels:  fork
promisify-child-process
seriously like the best async child process library
Stars: ✭ 54 (+107.69%)
Mutual labels:  exec
perfy
A simple, light-weight NodeJS utility for measuring code execution in high-resolution real times.
Stars: ✭ 54 (+107.69%)
Mutual labels:  execution

ch.vorburger.exec Maven Central Javadocs

This is a small library allowing to launch external processes from Java code in the background, and conveniently correctly pipe their output e.g. into slf4j, await either their termination or specific output, etc.

If you like/use this project, Sponsoring me or a Star / Watch / Follow me on GitHub is very much appreciated!

Release Notes are in CHANGES.md.

Usage

Launching external processes from Java using the raw java.lang.ProcessBuilder API directly can be a little cumbersome. Apache Commons Exec makes it a bit easier, but lacks some convenience. This library makes it truly convenient:

ManagedProcessBuilder pb = new ManagedProcessBuilder("someExec")
    .addArgument("arg1")
    .setWorkingDirectory(new File("/tmp"))
    .getEnvironment().put("ENV_VAR", "...")
    .setDestroyOnShutdown(true)
    .addStdOut(new BufferedOutputStream(new FileOutputStream(outputFile)))
    .setConsoleBufferMaxLines(7000);  // used by startAndWaitForConsoleMessageMaxMs

ManagedProcess p = pb.build();
p.start();
p.isAlive();
p.waitForExit();
// OR: p.waitForExitMaxMsOrDestroy(5000);
// OR: p.startAndWaitForConsoleMessageMaxMs("Successfully started", 3000);
p.exitValue();
// OR: p.destroy();

// This works even while it's running, not just when it exited
String output = p.getConsole();

If you need to, you can also attach a listener to get notified when the external process ends, by using setProcessListener() on the ManagedProcessBuilder with a ManagedProcessListener that implements onProcessComplete() and onProcessFailed().

We currently internally use Apache Commons Exec by building on top, extending and wrapping it, but without exposing this in its API, so that theoretically in the future this implementation detail could be changed.

Advantages

  • automatically logs external process's STDOUT and STDERR using SLF4j out of the box (can be customized)
  • automatically logs and throws for common errors (e.g. executable not found), instead of silently ignoring like j.l.Process
  • automatically destroys external process with JVM shutdown hook (can be disabled)
  • lets you await appearance of certain messages on the console
  • lets you write tests against the expected output

History

Historically, this code was part of MariaDB4j (and this is why it's initial version was 3.0.0), but was it later split into a separate project. This was done to make it usable in separate projects (originally to launch Ansible Networking CLI commands from OpenDaylight, later to manage etcd servers in tests, both from OpenDaylight); later for vexpect,

Similar Projects

For the exec functionality, zt-exec (with zt-process-killer) is similar (but refused to backlink us).

NuProcess is another similar library in the same space.

Related Projects

For the expect-like functionality, from https://en.wikipedia.org/wiki/Expect#Java, note (in no particular order):

Release

First test that GPG is set up correctly (gpg: no default secret key: No secret key gpg: signing failed: No secret key), and that the settings.xml has the credz for oss.sonatype.org (status: 401 unauthorized):

mvn verify -Pgpg

mvn deploy

Once that works, the next release can be done similarly similarly to https://github.com/vorburger/MariaDB4j#release:

mvn release:prepare
mvn release:perform -Pgpg
mvn release:clean
git push

If mvn release:prepare fails with the following error, then comment out forceSignAnnotated = true under [tag] in ~/.gitconfig:

The git-tag command failed.
[ERROR] Command output:
[ERROR] error: gpg failed to sign the data
[ERROR] error: unable to sign the tag

ToDo

This library is currently used to control daemon style external executables. To launch a process which returns binary (or massive textual) output to its STDOUT (and, presumably, have that piped into a java.io.OutputStream), it would need some tweaks. This would include making the enabled-by-default logging into slf4j, and the built-in startAndWaitForConsoleMessageMaxMs which collects output, a more configurable option.

Contributions & patches more than welcome!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].