2017-02-21 133 views
2

有没有什么方法可以在java中使用我们可以卸载的应用程序。我正在开发一个应用程序,我需要检查应用程序是否安装。如果安装了,那么首先我必须卸载应用程序并安装它的较新版本。使用java卸载应用程序

如果未安装,则直接进行安装。我所写的 代码是:

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

在这段代码中我正在删除的文件夹卸载,但事实并非解决方案的应用程序仍然存在。

+0

使用Java进行,这似乎是overcomplating的事情,因为你反正风运行命令。将使用powershell来代替。 http://stackoverflow.com/questions/113542/how-can-i-uninstall-an-application-using-powershell – Tobb

+0

由于minigeek提到你将不得不做两个步骤......其实3。 1)。检查是否有提及的应用程序的卸载程序并首先运行。 2)。检查注册表并删除它们,但是你将不得不知道注册表项是如何创建的,如果你破坏了这个注册表项,你可能会破坏整个机器。 3)。按照您当前的操作删除文件夹。 仍然不确定为什么你需要这样做,这绝对只适用于Windows。 祝你好运。 –

+0

@QuintonDelpeche是的。删除未知的注册表是潜在的危险。我添加了一个解决方案卸载(不完美,但它确实)。纠正我,如果我错了某个地方 – minigeek

回答

0

我不知道卸载(通用方法)也许有人知道。但有一个第三方Java API,您可以使用它来检索Windows注册表。 Windows中安装的每个应用程序都在注册表中注册。你可以在那里检查它。这里是Firefox的示例代码

要检查:特定的软件是否安装?

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

} 

这仅适用于Windows其他操作系统的bash需要采取行动。

如何卸载(只有当你知道卸载程序路径)

Process p = new ProcessBuilder("C:\\Program Files\\Mozilla Firefox\\uninstall.exe"); 
p.start(); 
+0

jRegistryKey.dll无法加载,因为它是32位...所以我想将我的jvm从64位降级到32位 – BleedCode

+0

@SRISHTI是啊。你将不得不移动32位jvm。 – minigeek

+0

@SRISHTI对你有帮助吗? – minigeek