문제

In ruby I have:

PTY.spawn("/usr/bin/lxc-monitor -n .+") do |i, o, pid|
  # ...
end

How do this in scala/java?

도움이 되었습니까?

해결책

Try JPty or pty4j. These are implementations of pty for Java using JNA.

다른 팁

I don't think that PTY has been ported to java/scala. You can use the built in Runtime from 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)
  }

I used this page as the base for the scala version.

UPDATE:

To get the output you need to get the input stream and read it (I know this sounds backwards but it's input relative to the jvm). The example below uses apache commons to skip some verbose parts of 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)
  }

}

I got the apache utils idea from here.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top