Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a program is installed on Windows system [duplicate]

Tags:

java

windows

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

like image 858
greenLizard Avatar asked Mar 13 '10 20:03

greenLizard


People also ask

How do I get rid of duplicate apps on Windows?

Alternatively, can right-click the Start button and select "Settings" from the list. In the Setting window click Apps from the left pane > select Apps & Features. Next, in the Apps & features window look for the Duplicate Files Fixer. Click the three dots next to it and select Uninstall from the context menu.


1 Answers

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.

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

}

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.

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

like image 120
BalusC Avatar answered Sep 26 '22 09:09

BalusC