windows 如何检查Windows系统上是否安装了程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2439984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:07:08  来源:igfitidea点击:

How to check if a program is installed on Windows system

javawindows

提问by greenLizard

How can I check with Java if a program is installed on a Windows system, for example to check for Mozilla Firefox?

如果程序安装在 Windows 系统上,我如何使用 Java 检查,例如检查 Mozilla Firefox?

采纳答案by BalusC

I assume that you're talking about Windows. As Java is intented to be a platform independent language and the way how to determine it differs per platform, there's no standard Java API to check that. You can however do it with help of JNIcalls on a DLL which crawls the Windows registry. You can then just check if the registry key associated with the software is present in the registry. There's a 3rd party Java API with which you can crawl the Windows registry: jRegistryKey.

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

Here's an SSCCEwith help of 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".
        }
    }

}

If you however intend to have a platformindependent application, then you'll also have to take into account the Linux/UNIX/Mac/Solaris/etc (in other words: anywhere where Java is able to run) ways to detect whether FF is installed. Else you'll have to distribute it as a Windows-only application and do a System#exit()along with a warning whenever System.getProperty("os.name")does not Windows.

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

Sorry, I don't know how to detect in other platforms whether FF is installed or not, so don't expect an answer from me for that ;)

抱歉,我不知道如何在其他平台上检测是否安装了 FF,所以不要指望我会回答这个问题;)

回答by brabster

There's no API I know of that will allow you to do this - I expect the most general method is to check file locations.

我知道没有任何 API 可以让您执行此操作 - 我希望最通用的方法是检查文件位置。

Other approaches (like checking Windows registry) are OS-dependent.

其他方法(如检查 Windows 注册表)取决于操作系统。