Builder

class Builder(val command: String)(source)

Creates a new Process.

e.g. (Shell commands on a Unix system)

val out = Process.Builder("sh")
    .args("-c")
    .args("sleep 1; exit 5")
    .destroySignal(Signal.SIGKILL)
    .stdin(Stdio.Null)
    .output { timeoutMillis = 1_500 }

assertEquals(5, out.processInfo.exitCode)

e.g. (Executable file)

val p = Process.Builder(myExecutableFile)
    .args("--some-flag")
    .args("someValue")
    .args("--another-flag", "anotherValue")
    .environment {
        remove("HOME")
        // ...
    }
    .stdin(Stdio.Null)
    .stdout(Stdio.File.of("logs/myExecutable.log", append = true))
    .stderr(Stdio.File.of("logs/myExecutable.err"))
    .spawn()

Parameters

command

The command to run. On Native Linux, macOS and iOS, if command is a relative file path or program name (e.g. ping) then posix_spawnp is utilized. If it is an absolute file path (e.g. /usr/bin/ping), then posix_spawn is utilized.

Constructors

Link copied to clipboard
constructor(executable: File)

Alternate constructor for an executable File. Will take the absolute + normalized path to use for command.

constructor(command: String)

Properties

Link copied to clipboard
@JvmField
val command: String

Functions

Link copied to clipboard
fun args(vararg args: String): Process.Builder
fun args(args: List<String>): Process.Builder

Add multiple arguments

fun args(arg: String): Process.Builder

Add a single argument

Link copied to clipboard

Changes the working directory of the spawned process.

Link copied to clipboard

Set the Signal to use when Process.destroy is called.

Link copied to clipboard

Configures the detached option for spawn.

Link copied to clipboard
fun environment(block: MutableMap<String, String>.() -> Unit): Process.Builder

Modify the environment via lambda

fun environment(key: String, value: String): Process.Builder

Set/overwrite an environment variable

Link copied to clipboard

Set a ProcessException.Handler to manage internal Process errors for spawned processes.

Link copied to clipboard
fun output(): Output
fun output(block: Output.Options.Builder.() -> Unit): Output

Blocks the current thread until Process completion, Output.Options.Builder.timeoutMillis is exceeded, or Output.Options.Builder.maxBuffer is exceeded.

Link copied to clipboard
fun Process.Builder.shell(enable: Boolean): Process.Builder

Configures the shell option for spawn and spawnSync

Configures the shell option for spawn and spawnSync.

Link copied to clipboard
fun spawn(): Process

Spawns the Process

inline fun <T> spawn(block: (process: Process) -> T): T

Spawns the Process and calls _signal upon block closure.

Link copied to clipboard
fun stderr(destination: Stdio): Process.Builder

Modify the standard error output destination

Link copied to clipboard

Modify the standard input source

Link copied to clipboard
fun stdout(destination: Stdio): Process.Builder

Modify the standard output destination

Link copied to clipboard

Configures the windowsHide option for spawn and spawnSync

Link copied to clipboard

Configures the windowsVerbatimArguments option for spawn and spawnSync