如何在Java中获得已安装软件及其版本的列表?
答案 0 :(得分:1)
有another question on StackOverflow检查是否存在特定软件。
您可以列出所有具有
的文件路径HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall
在开头,然后将此文件路径末尾的任何内容作为软件的名称。
答案 1 :(得分:0)
我认为以下代码可能会对您有所帮助。我是java的新手,所以如果有任何变化,请编辑我的代码。
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class getInstalledAppList {
private static final String REGQUERY_UTIL = "reg query ";
private static final String REGSTR_TOKEN = "REG_SZ";
static String s = REGQUERY_UTIL + "HKEY_LOCAL_MACHINE\\Software"
+ "\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
public static String getCurrentUserPersonalFolderPath() {
try {
Process process = Runtime.getRuntime().exec(s);
StreamReader reader = new StreamReader(process.getInputStream());
reader.start();
process.waitFor();
reader.join();
String result = reader.getResult();
int p = result.indexOf(REGSTR_TOKEN);
if (p == -1)
return null;
return result.substring(p + REGSTR_TOKEN.length()).trim();
}
catch (IOException | InterruptedException e) {
return null;
}
}
static class StreamReader extends Thread {
private final InputStream is;
private final StringWriter sw;
StreamReader(InputStream is) {
this.is = is;
sw = new StringWriter();
}
@Override
public void run() {
try {
int c;
while ((c = is.read()) != -1)
sw.write(c);
}
catch (IOException e) { e.printStackTrace(); }
}
String getResult() {
return sw.toString();
}
}
public static void main(String s[]) {
getDisplayNameDword( getCurrentUserPersonalFolderPath() );
}
private static void getDisplayNameDword(String str){
Set<String> set = new HashSet<>();
String [] array = new String[500];
array = str.split("\n");
for(String i : array){
set.add( getName(i.trim()) );
}
Iterator i = set.iterator();
while(i.hasNext()){
System.out.println( i.next() );
}
}
private static String getName(String s){
Process process = null;
try {
// Run reg query, then read output with StreamReader (internal class)
process = Runtime.getRuntime().exec("reg query " +
'"'+ s + "\" /v " + "DisplayName");
StreamReader reader = new StreamReader(process.getInputStream());
reader.start();
process.waitFor();
reader.join();
// Parse out the value
String[] parsed = reader.getResult().split(REGSTR_TOKEN);
if (parsed.length > 1) {
return (parsed[parsed.length-1]).trim();
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
}
return null;
}
}
或使用psinfo.exe在Windows中安装应用程序列表。
答案 2 :(得分:0)
我写了一个小图书馆(JavaListApps),它是根据ListPrograms进行的,而该图书馆又是基于 VB Script 从一个链接 StackOverflow Post
我的需求有限,所以与C ++版本相比,还有一些未实现的部分。