2

Is there any way in java by using which we can uninstall an application. I am developing an application by using which i need to check if application is installed or not. If it is installed then first i have to uninstall the application and install the newer version of it.

If it is not installed then directly go for installing. Code what i have written is:

String v = "C:\\Program Files\\InstalledFile";
    File file = new File(v);
    if(file.exists()==true)
    {
        System.out.print("file exist");
        FileUtils.deleteDirectory(file);
        System.out.print("deleted");
        Runtime run = Runtime.getRuntime();
        String msifile = "EP.msi";
        String para="rundll32 SHELL32.DLL,ShellExec_RunDLL msiexec /qb /i C:\\Setup\\EP.msi REBOOT=ReallySuppress";
        run.exec(para);
    }
    else
        System.out.print("file won't exist");

In this code i am deleting the folder for uninstalling but that is not the solution as application still exist.

Srishti
  • 355
  • 1
  • 17
  • Using java for this seems like overcomplating things, since you will wind up running commands anyways. Would use powershell instead. http://stackoverflow.com/questions/113542/how-can-i-uninstall-an-application-using-powershell – Tobb Feb 21 '17 at 08:30
  • As minigeek mentioned you are going to have to do two steps ... well 3 actually. 1). Check if there is an uninstaller for the app mentioned and running that first. 2). Check the registry and remove those but you are going to have to know what registry entries are created and if you break this you can break the whole machine. 3). Remove the folder as you are currently doing. Still not sure why you need to do it this way and this will definitely only work for Windows. Good Luck. – Quinton Delpeche Feb 21 '17 at 08:43
  • @QuintonDelpeche Yes. Removing unknown registry is potentially dangerous. I added a solution to uninstall(not perfect but yet it does). Correct me if I am wrong somewhere please – minigeek Feb 21 '17 at 09:03
  • This is very board with no clear question. – tak3shi Feb 21 '17 at 09:09

1 Answers1

0

I don't know about uninstalling(generic method) maybe someone knows.but there's a 3rd party Java API with which you can crawl the Windows registry. Every application installed in Windows is registered in registry. You can check it there . Here's sample code for firefox

To check: particular software is installed or not ?

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()); //look here for "Mozilla FireFox".here will be your body of uninstallation with some conditions
        }
    }

}

This is only for Windows for other os bash actions are required.

How to uninstall (only if you know uninstaller path)

 Process p = new ProcessBuilder("C:\\Program Files\\Mozilla Firefox\\uninstall.exe"); 
 p.start();
minigeek
  • 2,766
  • 1
  • 25
  • 35