2012-07-18 99 views

回答

1

another question on StackOverflow检查特定软件的存在。

您可以在一开始有

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall 

所有文件路径的列表,然后采取任何在这个文件路径的结尾是软件的名称。

+0

但不是它依赖于操作系统,因为我只能使它适用于Windows。但是如果我想要它与Linux一起工作呢?是否可以解析文件位置或搜索特定目录中的软件名称?任何帮助将非常感激。 – Spaniard89 2012-07-18 11:01:45

+0

对于Linux: 为了从Java运行Linux shell命令,请参见[示例](http://linuxprogram.wordpress.com/2006/10/22/how-to-excute-a-linux-shell-command -in-java /) 至于要运行什么:[看这里](http://linuxprogram.wordpress.com/2006/10/22/how-to-excute-a-linux-shell-command-in -java /) – Antimony 2012-07-18 11:12:19

+0

我非常抱歉,但如何解决我的问题? – Spaniard89 2012-07-18 11:14:43

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中获取已安装的应用程序列表。

相关问题