ProcessBuilder - 在 Scala 中启动另一个进程/JVM - 如何?

发布于 2024-09-10 11:05:38 字数 1070 浏览 5 评论 0原文

我已经用 Java 启动了另一个虚拟机。 请参阅 ProcessBuilder - 启动另一个进程/JVM - HowTo?

出于某种原因,我在 Scala 中无法做到同样的事情。 代码

object NewProcTest {
  def main(args :Array[String]) {
    println("Main")
//    val clazz = classOf[O3]
    val clazz = O4.getClass
    Proc.spawn(clazz, true)
    println("fin")
  }
}

object Proc{
  def spawn(clazz :Class[_],  redirectStream :Boolean)  {
    val separator = System.getProperty("file.separator")
    val classpath = System.getProperty("java.class.path")
    val path = System.getProperty("java.home") + 
      separator + "bin" + separator + "java"
    val processBuilder = 
            new ProcessBuilder(path, "-cp", 
            classpath, 
            clazz.getCanonicalName())
    processBuilder.redirectErrorStream(redirectStream)
    val process = processBuilder.start()
    process.waitFor()
    System.out.println("Fin")
  }
}

这是我尝试在对象和类中定义 main 的 。两者都在同一个 .scala 文件中或在单独的文件中。

我做错了什么?

I already handled to start another VM in Java.
See ProcessBuilder - Start another process / JVM - HowTo?

For some reason, I can't manage to do the same in Scala.
Here's my code

object NewProcTest {
  def main(args :Array[String]) {
    println("Main")
//    val clazz = classOf[O3]
    val clazz = O4.getClass
    Proc.spawn(clazz, true)
    println("fin")
  }
}

object Proc{
  def spawn(clazz :Class[_],  redirectStream :Boolean)  {
    val separator = System.getProperty("file.separator")
    val classpath = System.getProperty("java.class.path")
    val path = System.getProperty("java.home") + 
      separator + "bin" + separator + "java"
    val processBuilder = 
            new ProcessBuilder(path, "-cp", 
            classpath, 
            clazz.getCanonicalName())
    processBuilder.redirectErrorStream(redirectStream)
    val process = processBuilder.start()
    process.waitFor()
    System.out.println("Fin")
  }
}

I've tried to define the main in an object and in class. Both within the same .scala file or within a separate one.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情废物 2024-09-17 11:05:38

问题似乎是对象的类名有“$”后缀。

如果去掉该后缀,从 ProcessBuilder 触发的 Java 调用行就会起作用。

我在下面修改了一些内容来展示一些测试用例。我还不确定为什么会出现这种情况,但至少它提供了一种解决方法。

import java.io.{InputStreamReader, BufferedReader}
import System.{getProperty => Prop}

object O3 {def main(args: Array[String]) {println("hello from O3")}}

package package1 {
  object O4 {def main(args: Array[String]) {println("hello from O4")}}
}

object NewProcTest {
  val className1 = O3.getClass().getCanonicalName().dropRight(1)
  val className2 = package1.O4.getClass().getCanonicalName().dropRight(1)
  val sep        = Prop("file.separator")
  val classpath  = Prop("java.class.path")
  val path       = Prop("java.home")+sep+"bin"+sep+"java"

  println("className1 = " + className1)
  println("className2 = " + className2)

  def spawn(className:      String,
            redirectStream: Boolean) {
    val processBuilder = new ProcessBuilder(path, "-cp", classpath, className)
    val pbcmd          = processBuilder.command().toString()

    println("processBuilder = " + pbcmd)

    processBuilder.redirectErrorStream(redirectStream)

    val process = processBuilder.start()
    val reader  = new BufferedReader(new InputStreamReader(process.getInputStream()))

    println(reader.readLine())
    reader.close()
    process.waitFor()
  }

  def main(args :Array[String]) {
    println("start")
    spawn(className1, false)
    spawn(className2, false)
    println("end")
  }
}

The issue seems to be that the class name for an object has a '$' suffix.

If you strip off that suffix, the Java invocation line triggered from ProcessBuilder works.

I've hacked something below to show a couple of test cases. I'm not yet sure yet why this is the case but at least it provides a workaround.

import java.io.{InputStreamReader, BufferedReader}
import System.{getProperty => Prop}

object O3 {def main(args: Array[String]) {println("hello from O3")}}

package package1 {
  object O4 {def main(args: Array[String]) {println("hello from O4")}}
}

object NewProcTest {
  val className1 = O3.getClass().getCanonicalName().dropRight(1)
  val className2 = package1.O4.getClass().getCanonicalName().dropRight(1)
  val sep        = Prop("file.separator")
  val classpath  = Prop("java.class.path")
  val path       = Prop("java.home")+sep+"bin"+sep+"java"

  println("className1 = " + className1)
  println("className2 = " + className2)

  def spawn(className:      String,
            redirectStream: Boolean) {
    val processBuilder = new ProcessBuilder(path, "-cp", classpath, className)
    val pbcmd          = processBuilder.command().toString()

    println("processBuilder = " + pbcmd)

    processBuilder.redirectErrorStream(redirectStream)

    val process = processBuilder.start()
    val reader  = new BufferedReader(new InputStreamReader(process.getInputStream()))

    println(reader.readLine())
    reader.close()
    process.waitFor()
  }

  def main(args :Array[String]) {
    println("start")
    spawn(className1, false)
    spawn(className2, false)
    println("end")
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文