从 java 中查找 java.exe 位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19369769/
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 16:32:47  来源:igfitidea点击:

Find the java.exe location from java

javajvm

提问by Konstantin Tarashchanskiy

I am wondering if there is a way, from java, to find the location of the java.exe running the program?

我想知道是否有办法从 java 中找到运行程序的 java.exe 的位置?

My use case is that I am trying to have Java execute a batch script that needs JAVA_HOME to be set in the local environment. The environment that I am executing this on may not have JAVA_HOME set or even the java executable on the path, but I would assume that the JVM knows where its executable is located.

我的用例是我试图让 Java 执行一个需要在本地环境中设置 JAVA_HOME 的批处理脚本。我在其上执行此操作的环境可能没有设置 JAVA_HOME 甚至路径上的 java 可执行文件,但我假设 JVM 知道其可执行文件所在的位置。

采纳答案by Sage

 System.getProperty("java.home");

is one option. Which shows the following directory in my machine:

是一种选择。其中显示了我机器中的以下目录:

C:\Program Files\Java\jdk1.7.0_02\jre

C:\Program Files\Java\jdk1.7.0_02\jre

Most important system properties are listed here.

此处列出了最重要的系统属性

回答by hexacyanide

The java.homeproperty will supply the path to the currently running JRE, regardless of what is installed, or what JAVA_HOMEis. See the Oracle documentation here.

java.home属性将提供当前正在运行的 JRE 的路径,而不管安装了什么或安装了什么JAVA_HOME。请参阅此处的 Oracle 文档。

System.getProperty("java.home");