我该如何使用Java检查Windows系统上是否安装了某个程序,例如Mozilla Firefox?
这里是一个SSCCE,使用了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".
}
}
}
System.getProperty("os.name")
不是Windows时进行System#exit()
和警告提示。我不知道有任何API可以让你这样做 - 我认为最常见的方法是检查文件位置。
其他方法(如检查Windows注册表)取决于操作系统。