从java代码以编程方式查找绝对java.exe路径 [英] Find absolute java.exe path programmatically from java code

查看:421
本文介绍了从java代码以编程方式查找绝对java.exe路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

If I have a java jar or class file which is launched by the user (assuming java path is set in environment variables), so how can i from within the code, figure out absolute path of java.exe/javaw.exe from which this file is being launched.

就像在ubuntu上一样,我们可以运行:%java 并显示路径。

Like on ubuntu we can run: % which java and it shows the path.

但是在Windows上,如果我检查 System.getenv(),可能会发现有多个路径,例如旧版本或新版本。如果通过cmd行,我运行 java -version 它不会显示路径。

However on windows, if i check System.getenv() it may happen that there are multiple path's found e.g for old or new version. If through cmd line, I run java -version it does not show the path.

您能通过Windows上的纯java或命令行告诉我如何找到javaw.exe的位置?

Can you tell me either through pure java or command line on windows how is it possible to find out the location of javaw.exe?

推荐答案

String javaHome = System.getProperty("java.home");








你能说出来吗?我可以通过纯Java ...在Windows上如何找到javaw.exe的位置?

Can you tell me either through pure Java ... on windows how is it possible to find out the location of javaw.exe?



EG



E.G.

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());
    }
}



输出



Output

C:\Program Files (x86)\Java\jdk1.6.0_29\jre\bin\javaw.exe    exists: true
Press any key to continue . . .

是的,我相信这将适用于JRE。

And yes, I am confident that will work in a JRE.

这篇关于从java代码以编程方式查找绝对java.exe路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆