ProcessBuilder
or Runtime.getRuntime().exec
to execute external shell command :ProcessBuilder
This class is used to create operating system processes. Each ProcessBuilder instance manages a collection of process attributes. The start() method creates a new Process instance with those attributes. The start() method can be invoked repeatedly from the same instance to create new subprocesses with identical or related attributes.
fun processBuilder() {
val processBuilder = ProcessBuilder()
// -- Linux --
// Run a shell command
//processBuilder.command("bash", "-c", "ls /home/your_user/")
// Run a shell script
//processBuilder.command("path/to/hello.sh");
// -- Windows --
// Run a command
processBuilder.command("cmd.exe", "/c", "dir C:/Users/your_user");
// Run a bat file
//processBuilder.command("C:/Users/your_user/file.bat");
try {
val process: Process = processBuilder.start()
val reader = BufferedReader(InputStreamReader(process.inputStream))
val line = reader.readText();
println(line)
val exitVal = process.waitFor()
if (exitVal == 0) {
println("Success!")
//exitProcess(0)
} else {
//else...
}
} catch (e: IOException) {
e.printStackTrace()
} catch (e: InterruptedException) {
e.printStackTrace()
}
}
Runtime.getRuntime().exec()
Every Java application has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime method.
fun runtimeExec() {
try {
// -- Linux --
// Run a shell command
//val process = Runtime.getRuntime().exec("ls /home/your_user/")
// Run a shell script
//val process = Runtime.getRuntime().exec("path/to/hello.sh")
// -- Windows --
// Run a command
val process = Runtime.getRuntime().exec("cmd /c dir C:/Users/your_user")
//Run a bat file
//val process = Runtime.getRuntime().exec(
// "cmd /c hello.bat", null, File("C:/Users/your_user/"))
val reader = BufferedReader(
InputStreamReader(process.inputStream))
val line = reader.readText();
println(line)
val exitVal = process.waitFor()
if (exitVal == 0) {
println("Success!")
// exitProcess(0)
} else {
//else...
}
} catch (e: IOException) {
e.printStackTrace()
} catch (e: InterruptedException) {
e.printStackTrace()
}
}
Run & Test
hello.sh
#!/bin/sh
echo Hello World
fun main(args: Array<String>) {
val processBuilder = ProcessBuilder()
// -- Linux --
// Run a shell command
processBuilder.command("bash", "-c", "sh /home/hello.sh")
try {
val process: Process = processBuilder.start()
val reader = BufferedReader(InputStreamReader(process.inputStream))
val line = reader.readText();
println(line)
val exitVal = process.waitFor()
if (exitVal == 0) {
println("Success!")
//exitProcess(0)
} else {
//else...
}
} catch (e: IOException) {
e.printStackTrace()
} catch (e: InterruptedException) {
e.printStackTrace()
}
}
Output
References:
https://mkyong.com/java/how-to-execute-shell-command-from-java/
https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html
https://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html
No comments:
Post a Comment