ProcessBuilder - 在 Scala 中启动另一个进程/JVM - 如何?
我已经用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题似乎是对象的类名有“$”后缀。
如果去掉该后缀,从 ProcessBuilder 触发的 Java 调用行就会起作用。
我在下面修改了一些内容来展示一些测试用例。我还不确定为什么会出现这种情况,但至少它提供了一种解决方法。
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.