* this *真的是从Java代码启动第二个JVM的最佳方式吗? [英] Is *this* really the best way to start a second JVM from Java code?
问题描述
这是我上一个问题的后续内容,我有点尴尬地问这个...但无论如何:如何以独立于系统的方式从独立的Java程序启动第二个JVM?并且不依赖于例如像JAVA_HOME这样的env变量,因为它可能指向与当前正在运行的JRE不同的JRE。我提出了以下代码实际上有效,但感觉有点尴尬:
This is a followup to my own previous question and I'm kind of embarassed to ask this... But anyway: how would you start a second JVM from a standalone Java program in a system-independent way? And without relying on for instance an env variable like JAVA_HOME as that might point to a different JRE than the one that is currently running. I came up with the following code which actually works but feels just a little awkward:
public static void startSecondJVM() throws Exception {
String separator = System.getProperty("file.separator");
String classpath = System.getProperty("java.class.path");
String path = System.getProperty("java.home")
+ separator + "bin" + separator + "java";
ProcessBuilder processBuilder =
new ProcessBuilder(path, "-cp",
classpath,
AnotherClassWithMainMethod.class.getName());
Process process = processBuilder.start();
process.waitFor();
}
此外,当前运行的JVM可能已经启动了一些其他参数( - D,-X ......,......)第二个JVM不知道。
Also, the currently running JVM might have been started with some other parameters (-D, -X..., ...) that the second JVM would not know about.
推荐答案
目前尚不清楚我总是希望在启动辅助过程时使用完全相同的参数,类路径或其他任何东西(特别是-X类的东西 - 例如,为什么孩子需要与父元素相同的堆设置)。
It's not clear to me that you would always want to use exactly the same parameters, classpath or whatever (especially -X kind of stuff - for example, why would the child need the same heap settings as its parents) when starting a secondary process.
我更喜欢使用某种外部配置来为子节点定义这些属性。这是一个更多的工作,但我认为最终你需要灵活性。
I would prefer to use an external configuration of some sort to define these properties for the children. It's a bit more work, but I think in the end you will need the flexibility.
要查看可能的配置设置的范围,你可能会看到运行配置 Eclipse中的设置。有相当多的标签配置。
To see the extent of possible configuration settings you might look at thye "Run Configurations" settings in Eclipse. Quite a few tabs worth of configuration there.
这篇关于* this *真的是从Java代码启动第二个JVM的最佳方式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!