Determining location of JVM executable during runtime

后端 未结 5 1158
挽巷
挽巷 2020-12-18 19:07

How does one obtain the location of the executable of the currently running JVM during runtime? I would like to instantiate another JVM as a subprocess using the ProcessBuil

相关标签:
5条回答
  • 2020-12-18 19:36

    Following code obtains path to current java executable by using ProcessHandle.Info of current ProcessHandle.

    Result is wrapped in Optional since the access to command info may be restricted by operating system permissions.

    String javaExecutablePath = ProcessHandle.current()
        .info()
        .command()
        .orElseThrow();
    
    0 讨论(0)
  • 2020-12-18 19:39

    You are trying to fork the entire JVM.

    1. That is extremely inefficient, mainly because of the heaviness of yet another java process. If your heavily doing this, then your program is going to be really slow
    2. Threads exist for this very reason

    But if you really must, you can try just executing java -arguments directly, since most standard java installations put java on the cli path.

    0 讨论(0)
  • 2020-12-18 19:55

    This thread has an interesting discussion of the issue covering several platforms: Finding current executable's path without /proc/self/exe

    Given that discussion, it should be possible, if you really needed it, to write some JNI wrapper that #ifdef's the current platform and makes the proper native call.

    If you're only on Linux the '/proc/self/exe' is a symbolic link to the actual executable being run. This has the advantage of not relying on any environment variables (i.e. PATH or JAVA_HOME). But as I said, it's definitely not platform independent.

    0 讨论(0)
  • 2020-12-18 19:57

    Yes, there is a way to get the path of the JVM executable (if it exists). Include it in the configuration of the application. There are lots of ways to do that: Command line argument -- java myApp.Main /path/to/Java; Properties -- java -Dpath.to.java=/path/to/java; etc.

    If you want true platform independence, then your whole scheme is flawed because the existence of a JVM executable is not guaranteed. I could imagine a JVM with no need for a java executable.

    If you want 99.99% platform independence, then I think you have the tools needed.

    0 讨论(0)
  • 2020-12-18 19:58

    You could always just use os.name to check if the user is running Windows or not. That'll work on OS X, Linux and Windows at

    String jvm_location;
    if (System.getProperty("os.name").startsWith("Win")) {
        jvm_location = System.getProperties().getProperty("java.home") + File.separator + "bin" + File.separator + "java.exe";
    } else {
        jvm_location = System.getProperties().getProperty("java.home") + File.separator + "bin" + File.separator + "java";
    }
    
    0 讨论(0)
提交回复
热议问题