在红宝石中,我有:
PTY.spawn("/usr/bin/lxc-monitor -n .+") do |i, o, pid|
  # ...
end
如何在 scala/java 中做到这一点?
我不认为 PTY 已被移植到 java/scala。您可以使用 java 中的内置运行时。
  def run() {
    val rt = Runtime.getRuntime
    val cmds = Array("/usr/bin/lxc-monitor", "-n .+")
    val env = Array("TERM=VT100")
    val p1 = rt.exec(cmds, env)
  }
我将此页面用作 scala 版本的基础。
更新:
要获得输出,您需要获取输入流并读取它(我知道这听起来倒退,但它是相对于 jvm 的输入)。下面的示例使用 apache commons 来跳过 java 的一些冗长部分。
import java.io.StringWriter
import org.apache.commons.io.IOUtils
class runner {
  def run() {
    val rt = Runtime.getRuntime
    val cmds = Array("/usr/bin/lxc-monitor", "-n .+")
    val env = Array("TERM=VT100")
    val p1 = rt.exec(cmds, env)
    val inputStream = p1.getInputStream
    val writer = new StringWriter()
    IOUtils.copy(inputStream, writer, "UTF-8")
    val output = writer.toString()
    println(output)
  }
}
我从这里得到了 apache utils 的想法。