*这*真的是从 Java 代码启动第二个 JVM 的最佳方式吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1229605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:09:41  来源:igfitidea点击:

Is *this* really the best way to start a second JVM from Java code?

javajvmmultiprocessing

提问by Robert Petermeier

This is a followup to my own previous questionand 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:

这是我自己之前的问题的后续问题,我有点不好意思问这个……但无论如何:您将如何以独立于系统的方式从独立的 Java 程序启动第二个 JVM?并且不依赖于例如像 JAVA_HOME 这样的环境变量,因为它可能指向与当前正在运行的 JRE 不同的 JRE。我想出了以下实际有效但感觉有点尴尬的代码:

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();
}

Also, the currently running JVM might have been started with some other parameters (-D, -X..., ...) that the second JVM would not know about.

此外,当前运行的 JVM 可能已使用第二个 JVM 不知道的其他一些参数(-D、-X...、...)启动。

采纳答案by djna

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.

我不清楚在启动辅助进程时您是否总是希望使用完全相同的参数、类路径或其他任何东西(尤其是 -X 类型的东西 - 例如,为什么子进程需要与其父进程相同的堆设置)。

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.

我更喜欢使用某种外部配置来为子项定义这些属性。这需要更多的工作,但我认为最终您将需要灵活性。

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.

要查看可能的配置设置的范围,您可以查看 Eclipse 中的“运行配置”设置。那里有相当多的选项卡值得配置。

回答by Stephen C

I think that the answer is "Yes". This probably as good as you can do in Java using system independent code. But be aware that even this is only relativelysystem independent. For example, in some systems:

我认为答案是“是”。这可能与您在 Java 中使用系统独立代码所做的一样好。但请注意,即使这也只是相对系统独立的。例如,在某些系统中:

  1. the JAVA_HOME variable may not have been set,
  2. the command name used to launch a JVM might be different (e.g. if it is not a Sun JVM), or
  3. the command line options might be different (e.g. if it is not a Sun JVM).
  1. JAVA_HOME 变量可能尚未设置,
  2. 用于启动 JVM 的命令名称可能不同(例如,如果它不是 Sun JVM),或者
  3. 命令行选项可能不同(例如,如果它不是 Sun JVM)。

If I was aiming for maximum portability in launching a (second) JVM, I think I would do it using wrapper scripts.

如果我的目标是在启动(第二个)JVM 时实现最大的可移植性,我想我会使用包装器脚本来实现。

回答by Daniel Tripp

To find the java executable that your code is currently running under (i.e. the 'path' variable in your question's sample code) there is a utility method within apache ant that can help you. You don't have to build your code with ant - just use it as a library, for this one method.

要查找您的代码当前在其下运行的 java 可执行文件(即问题示例代码中的“路径”变量),apache ant 中有一个实用方法可以帮助您。您不必使用 ant 构建您的代码 - 只需将其用作库,对于这种方法。

It is:

这是:

org.apache.tools.ant.util.JavaEnvUtils.getJreExecutable("java")

org.apache.tools.ant.util.JavaEnvUtils.getJreExecutable("java")

It takes care of the sort of special cases with different JVM vendors that others have mentioned. (And looking at the source code for it, there are more special cases than I would have imagined.)

它处理其他人提到的不同 JVM 供应商的特殊情况。(查看它的源代码,特殊情况比我想象的要多。)

It's in ant.jar. ant is distributed under the Apache license so hopefully you can use it how you want without hassle.

它在 ant.jar 中。ant 是在 Apache 许可下分发的,因此希望您可以轻松地使用它。