如果我有一个由用户启动的 java jar 或类文件(假设在环境变量中设置了 java 路径),那么我如何从代码中找出 java.exe/javaw.exe 的绝对路径从中启动此文件。
就像在 ubuntu 上一样,我们可以运行:% which java
它会显示路径。
但是在 Windows 上,如果我检查 System.getenv()
,可能会发现多个路径,例如旧版本或新版本。如果通过 cmd 行,我运行 java -version
它不显示路径。
你能告诉我通过纯 java 或命令行在 Windows 上如何找到 javaw.exe 的位置吗?
最佳答案
String javaHome = System.getProperty("java.home");
Can you tell me either through pure Java ... on windows how is it possible to find out the location of javaw.exe?
例如
import java.io.File;
class JavawLocation {
public static void main(String[] args) {
String javaHome = System.getProperty("java.home");
File f = new File(javaHome);
f = new File(f, "bin");
f = new File(f, "javaw.exe");
System.out.println(f + " exists: " + f.exists());
}
}
输出
C:\Program Files (x86)\Java\jdk1.6.0_29\jre\bin\javaw.exe exists: true
Press any key to continue . . .
是的,我相信这会在 JRE 中发挥作用。
关于windows - 从 java 代码中以编程方式查找绝对 java.exe 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9006127/