Windows – 从java代码中编程查找绝对的java.exe路径

如果我有一个用户启动的 java jar或类文件(假设 java路径设置在环境变量中),那么我如何从代码中找出java.exe / javaw.exe的绝对路径文件正在启动.

像ubuntu一样,我们可以运行:%哪个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工作.

相关文章

Windows2012R2备用域控搭建 前置操作 域控主域控的主dns:自...
主域控角色迁移和夺取(转载) 转载自:http://yupeizhi.blo...
Windows2012R2 NTP时间同步 Windows2012R2里没有了internet时...
Windows注册表操作基础代码 Windows下对注册表进行操作使用的...
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的...
一个简单的Windows Socket可复用框架说起网络编程,无非是建...