如何找到javaw.exe的安装位置?

时间:2013-07-11 19:46:30

标签: java windows javaw

因此,对于我正在进行的项目,我需要找出javaw.exe在用户计算机上的位置。 我该怎么做? 假设用户在Windows计算机上

我使用的方法仅限于Windows的英文版本 我找了安装操作系统的位置,找到Program Files目录,找到Javajdk目录,bin,然后找到javaw.exe。我知道这不适用于非英语版本的Windows。

与人类语言无关的方法是什么?

5 个答案:

答案 0 :(得分:9)

为了完整起见,请允许我提一下,如果路径中找不到,有些地方(在Windows PC上)要查找javaw.exe: (仍然是Reimeus的建议应该是你的第一次尝试。)

<强> 1
Java通常在注册表中存储它的位置,在以下键下:
HKLM\Software\JavaSoft\Java Runtime Environement\<CurrentVersion>\JavaHome

<强> 2
较新版本的JRE / JDK似乎也在{C:\ Windows \ System32'中放置了javaw.exe的副本,因此人们可能也想在那里检查一下(尽管有可能,如果它存在,它将是也在路径中找到了。)

第3
当然,有“通常”的安装位置:

  • 'C:\ Program Files \ Java \ jre * \ bin'
  • 'C:\ Program Files \ Java \ jdk * \ bin'
  • 'C:\ Program Files(x86)\ Java \ jre * \ bin'
  • 'C:\ Program Files(x86)\ Java \ jdk * \ bin'

[注意,对于旧版本的Windows(XP,Vista(?)),这只会对英文版操作系统有所帮助。幸运的是,在Windows的更高版本中,“程序文件”将指向该目录,而不管其“显示名称”(这是特定于语言的)。]

不久前,我写了这段代码来检查上述地方的javaw.exe。也许有人发现它很有用:

static protected String findJavaw() {
    Path pathToJavaw = null;
    Path temp;

    /* Check in Registry: HKLM\Software\JavaSoft\Java Runtime Environement\<CurrentVersion>\JavaHome */
    String keyNode = "HKLM\\Software\\JavaSoft\\Java Runtime Environment";
    List<String> output = new ArrayList<>();
    executeCommand(new String[] {"REG", "QUERY", "\"" + keyNode + "\"", 
                                 "/v", "CurrentVersion"}, 
                   output);
    Pattern pattern = Pattern.compile("\\s*CurrentVersion\\s+\\S+\\s+(.*)$");
    for (String line : output) {
        Matcher matcher = pattern.matcher(line);
        if (matcher.find()) {
            keyNode += "\\" + matcher.group(1);
            List<String> output2 = new ArrayList<>();
            executeCommand(
                    new String[] {"REG", "QUERY", "\"" + keyNode + "\"", 
                                  "/v", "JavaHome"}, 
                    output2);
            Pattern pattern2 
                    = Pattern.compile("\\s*JavaHome\\s+\\S+\\s+(.*)$");
            for (String line2 : output2) {
                Matcher matcher2 = pattern2.matcher(line2);
                if (matcher2.find()) {
                    pathToJavaw = Paths.get(matcher2.group(1), "bin", 
                                            "javaw.exe");
                    break;
                }
            }
            break;
        }
    }
    try {
        if (Files.exists(pathToJavaw)) {
            return pathToJavaw.toString();
        }
    } catch (Exception ignored) {}

    /* Check in 'C:\Windows\System32' */
    pathToJavaw = Paths.get("C:\\Windows\\System32\\javaw.exe");
    try {
        if (Files.exists(pathToJavaw)) {
            return pathToJavaw.toString();
        }
    } catch (Exception ignored) {}

    /* Check in 'C:\Program Files\Java\jre*' */
    pathToJavaw = null;
    temp = Paths.get("C:\\Program Files\\Java");
    if (Files.exists(temp)) {
        try (DirectoryStream<Path> dirStream 
                = Files.newDirectoryStream(temp, "jre*")) {
            for (Path path : dirStream) {
                temp = Paths.get(path.toString(), "bin", "javaw.exe");
                if (Files.exists(temp)) {
                    pathToJavaw = temp;
                    // Don't "break", in order to find the latest JRE version
                }                    
            }
            if (pathToJavaw != null) {
                return pathToJavaw.toString();
            }
        } catch (Exception ignored) {}
    }

    /* Check in 'C:\Program Files (x86)\Java\jre*' */
    pathToJavaw = null;
    temp = Paths.get("C:\\Program Files (x86)\\Java");
    if (Files.exists(temp)) {
        try (DirectoryStream<Path> dirStream 
                = Files.newDirectoryStream(temp, "jre*")) {
            for (Path path : dirStream) {
                temp = Paths.get(path.toString(), "bin", "javaw.exe");
                if (Files.exists(temp)) {
                    pathToJavaw = temp;
                    // Don't "break", in order to find the latest JRE version
                }                    
            }
            if (pathToJavaw != null) {
                return pathToJavaw.toString();
            }
        } catch (Exception ignored) {}
    }

    /* Check in 'C:\Program Files\Java\jdk*' */
    pathToJavaw = null;
    temp = Paths.get("C:\\Program Files\\Java");
    if (Files.exists(temp)) {
        try (DirectoryStream<Path> dirStream 
                = Files.newDirectoryStream(temp, "jdk*")) {
            for (Path path : dirStream) {
                temp = Paths.get(path.toString(), "jre", "bin", "javaw.exe");
                if (Files.exists(temp)) {
                    pathToJavaw = temp;
                    // Don't "break", in order to find the latest JDK version
                }                    
            }
            if (pathToJavaw != null) {
                return pathToJavaw.toString();
            }
        } catch (Exception ignored) {}
    }

    /* Check in 'C:\Program Files (x86)\Java\jdk*' */
    pathToJavaw = null;
    temp = Paths.get("C:\\Program Files (x86)\\Java");
    if (Files.exists(temp)) {
        try (DirectoryStream<Path> dirStream 
                = Files.newDirectoryStream(temp, "jdk*")) {
            for (Path path : dirStream) {
                temp = Paths.get(path.toString(), "jre", "bin", "javaw.exe");
                if (Files.exists(temp)) {
                    pathToJavaw = temp;
                    // Don't "break", in order to find the latest JDK version
                }                    
            }
            if (pathToJavaw != null) {
                return pathToJavaw.toString();
            }
        } catch (Exception ignored) {}
    }

    return "javaw.exe";   // Let's just hope it is in the path :)
}

答案 1 :(得分:3)

试试这个

for %i in (javaw.exe) do @echo. %~$PATH:i

答案 2 :(得分:3)

找到&#34; javaw.exe&#34;在Windows中我会使用(使用批处理)

for /f tokens^=2^ delims^=^" %%i in ('reg query HKEY_CLASSES_ROOT\jarfile\shell\open\command /ve') do set JAVAW_PATH=%%i

它适用于Windows XP和Seven,适用于JRE 1.6和1.7。应该赶上最新安装的版本。

答案 3 :(得分:1)

对我有用:

    String javaHome = System.getProperty("java.home");
    File f = new File(javaHome);
    f = new File(f, "bin");
    f = new File(f, "javaw.exe"); //or f = new File(f, "javaws.exe"); //work too
    System.out.println(f + "    exists: " + f.exists());

答案 4 :(得分:0)

打开cmd shell,

cd \\
dir javaw.exe /s