【问题标题】:Java check if program is installed on windowsJava检查程序是否安装在Windows上
【发布时间】:2016-03-16 08:59:13
【问题描述】:

有没有办法检查特定程序是否使用 Java 安装在 Windows 上?

我正在尝试开发一个 Java 程序,该程序通过使用 7-Zip 中的代码行命令自动创建 zip 存档。

所以,如果我的 Windows 操作系统上已经安装了“7-Zip”,我想检查 Java。不检查正在运行的应用程序或操作系统是 Windows 还是 Linux。如果在 Windows 上安装了“7-Zip”,我想得到一个布尔值(真/假)。

【问题讨论】:

标签: java windows zip


【解决方案1】:

Apache Commons 库有一个名为 SystemUtils 的类 - 完整文档可在 https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/SystemUtils.html 获得。

在这个库中,您可以使用以下static boolean 属性:

SystemUtils.IS_OS_LINUX
SystemUtils.IS_OS_WINDOWS

【讨论】:

  • 我想你误会了 :) 我不想检查,如果这是 Windows,我想检查是否安装了 ON MY WINDOWS '7-Zip'
【解决方案2】:

类 unix 的解决方案是简单地尝试使用 --version 标志运行程序(在 Windows 上可能是 /? 或 - 就像在 7zip 案例中 - 根本没有任何标志)并检查它是否失败,或者返回码是什么。

类似:

public boolean is7zipInstalled() {
    try {
            Process process = Runtime.getRuntime().exec("7zip.exe");
            int code = process.waitFor();
            return code == 0;
    } catch (Exception e) {
            return false;
    }
}

【讨论】:

    【解决方案3】:

    我假设您说的是 Windows。由于 Java 旨在成为一种独立于平台的语言,并且如何确定它的方式因平台而异,因此没有标准的 Java API 可以检查这一点。但是,您可以借助爬取 Windows 注册表的 DLL 上的 JNI 调用来完成此操作。然后,您只需检查与该软件关联的注册表项是否存在于注册表中。您可以使用第 3 方 Java API 来抓取 Windows 注册表:jRegistryKey。

    这是一个借助 jRegistryKey 的 SSCCE:

    package com.stackoverflow.q2439984;
    
    import java.io.File;
    import java.util.Iterator;
    
    import ca.beq.util.win32.registry.RegistryKey;
    import ca.beq.util.win32.registry.RootKey;
    
    public class Test {
    
        public static void main(String... args) throws Exception {
            RegistryKey.initialize(Test.class.getResource("jRegistryKey.dll").getFile());
            RegistryKey key = new RegistryKey(RootKey.HKLM, "Software\\Mozilla");
            for (Iterator<RegistryKey> subkeys = key.subkeys(); subkeys.hasNext();) {
                RegistryKey subkey = subkeys.next();
                System.out.println(subkey.getName()); // You need to check here if there's anything which matches "Mozilla FireFox".
            }
        }
    
    }
    

    但是,如果您打算拥有一个独立于平台的应用程序,那么您还必须考虑 Linux/UNIX/Mac/Solaris/等。 (换句话说:可以运行 Java 的任何地方)检测是否安装了 FF 的方法。否则,您必须将其作为仅限 Windows 的应用程序分发,并在 System.getProperty("os.name") 不是 Windows 时发出System#exit() 以及警告。

    对不起,我不知道如何在其他平台上检测是否安装了 FF,所以不要指望我的回答;)

    【讨论】:

    猜你喜欢
    • 2014-10-26
    • 1970-01-01
    • 1970-01-01
    • 2020-10-20
    • 2020-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多