简体   繁体   English

从 java 代码中以编程方式查找绝对 java.exe 路径

[英]Find absolute java.exe path programmatically from java code

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.如果我有一个由用户启动的 java jar 或 class 文件(假设在环境变量中设置了 java 路径),那么我如何从代码中找出 java.exe/javaw.exe 的绝对路径正在启动文件。

Like on ubuntu we can run: % which java and it shows the path.就像在 ubuntu 上一样,我们可以运行: % which java它会显示路径。

However on windows, if i check System.getenv() it may happen that there are multiple path's found eg for old or new version.但是在 windows 上,如果我检查System.getenv()可能会发现有多个路径,例如旧版本或新版本。 If through cmd line, I run java -version it does not show the path.如果通过 cmd 行,我运行java -version它不显示路径。

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?您能否通过纯 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? 你能告诉我通过纯Java ...在Windows上如何找到javaw.exe的位置?

EG 例如

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 . . .

And yes, I am confident that will work in a JRE. 是的,我相信这将在JRE中发挥作用。

On Windows, the java.library.path System Property begins with the path to the bin directory containing whichever java.exe was used to run your jar file. 在Windows上,java.library.path系统属性以bin目录的路径开头,该目录包含用于运行jar文件的java.exe。

This makes sense, because on Windows the first place any executable looks for DLL files is the directory containing the executable itself. 这是有道理的,因为在Windows上,任何可执行文件查找DLL文件的第一个位置是包含可执行文件本身的目录。 So naturally, when the JVM runs, the first place it looks for DLLs is the directory containing java.exe. 当然,当JVM运行时,它首先查找DLL的是包含java.exe的目录。

You can acquire the path to java.exe as follows: 您可以获取java.exe的路径,如下所示:

final String javaLibraryPath = System.getProperty("java.library.path");
final File javaExeFile = new File(
  javaLibraryPath.substring(0, javaLibraryPath.indexOf(';')) + "\\java.exe"
);
final String javaExePath =
  javaExeFile.exists() ? javaExeFile.getAbsolutePath() : "java";

This code is Windows-specific - I hard-coded the path separator (;) and the file separator (). 此代码是特定于Windows的 - 我对路径分隔符(;)和文件分隔符()进行了硬编码。 I also put in a fallback to just "java" in case the library path trick somehow doesn't work. 如果库路径技巧以某种方式不起作用的话,我也只是对“java”进行了回退。

I have tested this with Java 6 and 7 on Windows 7. I tried a 32-bit and 64-bit version of Java. 我在Windows 7上使用Java 6和7对此进行了测试。我尝试了32位和64位版本的Java。

Here's a slightly more generalised solution that I came up with. 这是我提出的稍微更通用的解决方案。 Maybe useful: 也许有用:

private static String javaExe()
{
    final String JAVA_HOME = System.getProperty("java.home");
    final File BIN = new File(JAVA_HOME, "bin");
    File exe = new File(BIN, "java");

    if (!exe.exists())
    {
        // We might be on Windows, which needs an exe extension
        exe = new File(BIN, "java.exe");
    }

    if (exe.exists())
    {
        return exe.getAbsolutePath();
    }

    try
    {
        // Just try invoking java from the system path; this of course
        // assumes "java[.exe]" is /actually/ Java
        final String NAKED_JAVA = "java";
        new ProcessBuilder(NAKED_JAVA).start();

        return NAKED_JAVA;
    }
    catch (IOException e)
    {
        return null;
    }
}

an issue with using "System.getProperty("java.home");"使用"System.getProperty("java.home");"的问题, is that it is not always the java exe that the jar is running on, if you want to get that, you can use "System.getProperty("sun.boot.library.path");" , 是它并不总是运行 jar 的 java exe,如果你想得到它,你可以使用"System.getProperty("sun.boot.library.path");" , from there you can find "java", "java.exe", "javaw", or "javaw.exe"... However there is still an issue with this, java will run just fine if the executable has been renamed, and the actual java executable's structure changes from different JRE's/JDKS's, so there is not much way to find the java exe if it has been renamed. ,从那里你可以找到“java”,“java.exe”,“javaw”或“javaw.exe”......但是这仍然存在问题,如果可执行文件已重命名,java 将运行正常,并且实际的 java 可执行文件的结构因不同的 JRE/JDKS 而异,因此如果已重命名,则没有太多方法可以找到 java exe。 unless someone else has a method ofc, in which case, can you share?除非别人有方法 ofc,在这种情况下,你能分享吗? :) :)

(Also, I have seen some people suggest using the first index of System.getProperty("java.library.path"); , note, this might not work if the user/launcher has manually set the library path, something which is not too uncommon) (此外,我看到有人建议使用System.getProperty("java.library.path");的第一个索引,请注意,如果用户/启动器手动设置了库路径,这可能不起作用,而这不是太罕见了)

Compilation of All above methods以上所有方法的汇编

static String getJavaPath(){

        String tmp1 = System.getProperty("java.home") + "\\bin\\java.exe";
        String tmp2 = System.getProperty("sun.boot.library.path") + "\\java.exe";
        String tmp3 = System.getProperty("java.library.path")+ "\\java.exe";
        if(new File(tmp1).exists()) {
            return tmp1;
        }else if(new File(tmp2).exists()){
            return tmp2;
        }else if(new File(tmp3).exists()) {
            return tmp3;
        }else{
            String[] paths = System.getenv("PATH").split(";");
            for(String path:paths){
                if(new File(path + "\\java.exe").exists()){
                    return path + "\\java.exe";
                }
            }
        }
        return "";
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM