2

Is there a way to check if a specific program is installed on Windows using Java?

I'm trying to develop a Java program that automatically creates zip archives by using the code line command from 7-Zip.

So, I would like to check in Java if on my windows OS '7-Zip' is already installed. No check for running apps or if OS is Windows or Linux. I want to get a bool (true/false) if '7-Zip' is installed on Windows.

Dennis Konoppa
  • 189
  • 1
  • 3
  • 13
  • 1
    Possible duplicate of [How to check if a program is installed on Windows system](http://stackoverflow.com/questions/2439984/how-to-check-if-a-program-is-installed-on-windows-system) – fabian Mar 16 '16 at 10:51

3 Answers3

0

The library Apache Commons has a class called SystemUtils - full documentation is available at https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/SystemUtils.html.

In this library you have the following static boolean properties at your disposal:

SystemUtils.IS_OS_LINUX
SystemUtils.IS_OS_WINDOWS
  • 1
    I think you misunderstand me :) I don't want to check, if this is windows, I want to check if ON MY WINDOWS '7-Zip' is installed – Dennis Konoppa Mar 16 '16 at 09:17
0

The unix-like solution would be to simply try to run the program with --version flag (on windows probably the /? or - like in the 7zip case - without any at all) and check whether it fails, or what the return code will be.

Something like:

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

I assume that you're talking about Windows. As Java is intended 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") is 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 ;)

ig0774
  • 39,669
  • 3
  • 55
  • 57
Devvrat
  • 1
  • 1
  • 3
    The answer was taken from https://stackoverflow.com/a/2440028/7113238 but didn't show any reference from the original source. – Lawliet Jul 09 '17 at 05:16