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

后端 未结 3 1324
囚心锁ツ
囚心锁ツ 2020-12-02 09:08

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 sy

3条回答
  •  星月不相逢
    2020-12-02 09:42

    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.

    It is:

    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.)

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

提交回复
热议问题