I want to invoke an external command from Kotlin code.
system()
function.But how do I do this in Kotlin?
Example of running a git diff by shelling out:
"git diff".runCommand(gitRepoDir)
Here are two implementations of the runCommand
extension function:
This wires any output from the subprocess to regular stdout and stderr:
fun String.runCommand(workingDir: File) { ProcessBuilder(*split(" ").toTypedArray()) .directory(workingDir) .redirectOutput(Redirect.INHERIT) .redirectError(Redirect.INHERIT) .start() .waitFor(60, TimeUnit.MINUTES) }
An alternative implementation redirecting to Redirect.PIPE
instead allows you to capture output in a String
:
fun String.runCommand(workingDir: File): String? { try { val parts = this.split("\\s".toRegex()) val proc = ProcessBuilder(*parts.toTypedArray()) .directory(workingDir) .redirectOutput(ProcessBuilder.Redirect.PIPE) .redirectError(ProcessBuilder.Redirect.PIPE) .start() proc.waitFor(60, TimeUnit.MINUTES) return proc.inputStream.bufferedReader().readText() } catch(e: IOException) { e.printStackTrace() return null } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With