Question

In ruby I have:

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

How do this in scala/java?

Était-ce utile?

La solution

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

Autres conseils

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top