使用的语言是scala,也就是对指令按空格分拆后,放进去执行
def getCmd(cmd: String): java.util.List[String] = {
import scala.collection.convert.wrapAsJava.bufferAsJavaList
bufferAsJavaList(cmd.split(" ").filter(_ != "").toBuffer)
}
def exec_detail(cmd: String): (Array[String], Array[String]) = {
import java.io.InputStreamReader
import java.io.BufferedReader
import scala.collection.mutable.ArrayBuffer
val inputBuffer: ArrayBuffer[String] = ArrayBuffer()
val errorBuffer: ArrayBuffer[String] = ArrayBuffer()
try {
val proc = new ProcessBuilder()
val process = proc.command(getCmd(cmd)).start()
val input = new BufferedReader(new InputStreamReader(process.getInputStream))
val error = new BufferedReader(new InputStreamReader(process.getErrorStream))
var line: String = input.readLine()
while (line != null) {
inputBuffer += line
line = input.readLine()
}
input.close()
line = error.readLine()
while (line != null) {
errorBuffer += line
line = error.readLine()
}
error.close()
} catch {
case e: Throwable => e.printStackTrace()
}
(inputBuffer.toArray, errorBuffer.toArray)
}