I need to exec a jar file and redirect the output from my executed process to the output of my main process.
I using the following code :
val command = "java.exe -version"
val p = Runtime.getRuntime().exec(command)
val buf = p.getInputStream()
val inputAsString = buf.bufferedReader().use { it.readText() }
println(inputAsString)
I have no output...
I have tested this code :
val command = "cmd /c chcp"
val p = Runtime.getRuntime().exec(command)
val sc = Scanner(p.inputStream)
println(sc.nextLine())
sc.close()
I have an output but when I replace "cmd /c chcp" I have an error...
How can I read the output of "test.jar" which write "ok" for example ?
java -version). Have you tried reading from there:val buf = p.errorStream?myfile.jarprints to stderr or stdout? You should consider using ProcessBuilder instead ofRuntime.exec, because then you can choose to redirect stderr to stdout so you only have to read from the 1 stream.