2012-07-18 76 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中獲取已安裝的應用程序列表。

相關問題