java 在运行时确定 JVM 可执行文件的位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3392416/
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
Determining location of JVM executable during runtime
提问by Samad Lotia
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 ProcessBuilder class.
如何在运行时获取当前运行的 JVM 的可执行文件的位置?我想使用 ProcessBuilder 类将另一个 JVM 实例化为子进程。
I am aware that there is the java.homeSystem property, but this doesn't specify the location of the JVM executable. I understand I could do something like this to get the path:
我知道有java.homeSystem 属性,但这并没有指定 JVM 可执行文件的位置。我知道我可以做这样的事情来获得路径:
System.getProperties().getProperty("java.home") + File.pathSeparator + "bin" + File.pathSeparator + "java"
System.getProperties().getProperty("java.home") + File.pathSeparator + "bin" + File.pathSeparator + "java"
This code isn't platform independent, because the Windows executable's name is java.exe, not java. Is there a way to get the path of the JVM executable that takes the platform's idiosyncrasies into account?
此代码与平台无关,因为 Windows 可执行文件的名称是java.exe,而不是java. 有没有办法获取考虑平台特性的 JVM 可执行文件的路径?
回答by Xenxier
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
您始终可以使用 os.name 来检查用户是否正在运行 Windows。这将适用于 OS X、Linux 和 Windows
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";
}
回答by pedorro
This thread has an interesting discussion of the issue covering several platforms: Finding current executable's path without /proc/self/exe
该线程对涵盖多个平台的问题进行了有趣的讨论: 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.
鉴于该讨论,如果您确实需要,应该可以编写一些 JNI 包装器,使 #ifdef 是当前平台并进行适当的本机调用。
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.
如果您只使用 Linux,则“/proc/self/exe”是指向正在运行的实际可执行文件的符号链接。这具有不依赖任何环境变量(即 PATH 或 JAVA_HOME)的优点。但正如我所说,它绝对不是独立于平台的。
回答by emory
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.
是的,有一种方法可以获取 JVM 可执行文件的路径(如果存在)。将其包含在应用程序的配置中。有很多方法可以做到这一点: 命令行参数 -- java myApp.Main /path/to/Java; 属性——java -Dpath.to.java=/path/to/java; 等等。
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.
如果您想要真正的平台独立性,那么您的整个方案就有缺陷,因为无法保证 JVM 可执行文件的存在。我可以想象一个不需要 java 可执行文件的 JVM。
If you want 99.99% platform independence, then I think you have the tools needed.
如果您想要 99.99% 的平台独立性,那么我认为您拥有所需的工具。
回答by czerny
Following code obtains path to current javaexecutable by using ProcessHandle.Infoof current ProcessHandle.
以下代码java通过使用ProcessHandle.Infocurrent获取当前可执行文件的路径ProcessHandle。
Result is wrapped in Optionalsince the access to command info may be restricted by operating system permissions.
Optional由于对命令信息的访问可能受到操作系统权限的限制,因此结果被包含在内。
String javaExecutablePath = ProcessHandle.current()
.info()
.command()
.orElseThrow();
回答by TheLQ
You are trying to fork the entire JVM.
您正在尝试分叉整个 JVM。
- 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
- Threads exist for this very reason
- 这是非常低效的,主要是因为另一个 java 进程的繁重。如果你大量这样做,那么你的程序将会非常缓慢
- 线程的存在正是出于这个原因
But if you reallymust, you can try just executing java -argumentsdirectly, since most standard java installations put java on the cli path.
但是如果你真的必须,你可以尝试直接执行java -arguments,因为大多数标准的 java 安装将 java 放在 cli 路径上。

