2012-07-18 3 views

답변

1

특정 소프트웨어의 존재 여부를 확인하는 another question on StackOverflow이 있습니다.

당신은 소프트웨어의 이름이 파일 경로의 끝에 무엇이든지 가지고, 처음에

HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Uninstall 

있는 모든 filepaths의 목록을 만들 수 있습니다.

+0

OS 의존적 인 것은 아니기 때문에 Windows에서만 작동하도록 만들 수 있습니다. 하지만 내가 리눅스에서도 잘 작동하려면 어떻게해야 할까? 파일 위치를 파싱하거나 특정 디렉토리에서 소프트웨어 이름을 검색 할 수 있습니까? 어떤 도움이라도 대단히 감사 할 것입니다. – Spaniard89

+0

Linux의 경우 : Java에서 Linux 셸 명령을 실행하려면 [예제] (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

+0

매우 유감 스럽지만이 문제는 어떻게 게시됩니까? – Spaniard89

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에 설치된 응용 프로그램 목록을 가져 오십시오.

관련 문제