如何检查Windows系统上是否安装了程序 [英] How to check if a program is installed on Windows system

查看:430
本文介绍了如何检查Windows系统上是否安装了程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在Windows系统上安装了某个程序,我该如何检查Java,例如检查Mozilla Firefox?

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

推荐答案

<我假设你在谈论Windows。由于Java旨在成为一种独立于平台的语言,并且每种平台的确定方式各不相同,因此没有标准的Java API来检查它。但是,您可以在DLL上 JNI 调用的帮助下完成此操作它抓取Windows注册表。然后,您可以检查注册表中是否存在与该软件关联的注册表项。您可以使用第三方Java API来抓取Windows注册表: jRegistryKey

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

这是jRegistryKey帮助下的 SSCCE

Here's an SSCCE with help of jRegistryKey:

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时执行 System#exit()以及警告os.name)不是Windows。

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.

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

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

这篇关于如何检查Windows系统上是否安装了程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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