2011-12-13 124 views
0

我在一個Java項目的工作爲我自己學習的Ubuntu的軟件工具,有什麼我已經是一類可以讀取和使用寫入外部進程Runtime.getRuntime().exec(cmd);如何以編程方式檢查是否安裝使用Java

現在我想知道是否有任何特殊的方法來檢查系統上是否安裝了特定的軟件/工具。

就像我使用sshpass實用程序來遠程登錄到其他機器,如果它不是已經存在,我想用我的程序安裝它。但是,對於這個我應該如何去檢查它是否存在或不存在?

我腦海中的想法是運行該命令並查看響應,如果返回的字符串匹配特定的表達式,則基於該信息我會判定它是存在還是不存在。

你認爲這是正確的方法還是有任何其他方式來找出這個?

就像在Windows中,我認爲有像FTYPE,assoc命令等CMDLINE公用事業, 謝謝,

回答

1

如果您已經知道軟件二進制文件的名稱(通常與進程名稱相同),則可以使用which命令。

您可以在bash測試/殼 其中火狐 在/ usr/bin中/火狐

此外,我可以爲您提供寫在bash輸出讀數的C#示例:

字符串輸出=字符串。空的;

string output = string.Empty; 

try 
{ 
    // Sets up our process, the first argument is the command 
    // and the second holds the arguments passed to the command 
    ProcessStartInfo ps = new ProcessStartInfo("bash"); 
    ps.Arguments = "-c 'firefox'"; 
    ps.UseShellExecute = false; 

    // Redirects the standard output so it reads internally in out program 
    ps.RedirectStandardOutput = true; 

    // Starts the process 
    using (Process p = Process.Start(ps)) 
    { 
     // Reads the output to a string 
     output = p.StandardOutput.ReadToEnd(); 

     // Waits for the process to exit must come *after* StandardOutput is "empty" 
     // so that we don't deadlock because the intermediate kernel pipe is full. 
     p.WaitForExit(); 
    } 
} 
catch 
{ 
    // TODO manage errors 
} 

如果bash的輸出是多行的管道,你可以預先篩選它grep命令:

ps.Arguments = "-c 'cpuid | grep MySearchTerm'"; 

編輯1:回覆評論

的主要問題是需要「管理」權限的軟件安裝。 我試圖創建一個解決辦法,但下面的行打破了所有代碼:

process = Runtime.getRuntime().exec(new String[]{"/bin/bash","-c","'echo RIadminXsrv1 | sudo -S apt-get install telnet -qy'"}); 

而在終端以下命令將實際嘗試安裝遠程登錄(你可能需要用戶插入/etc/sudoers重現它在你的電腦上)。

/bin/echo myUserPass | /usr/bin/sudo -S /usr/bin/apt-get install telnet -qy

在java中它會簡單地打印(echo輸出)命令的剩餘部分:

myUserPass | /usr/bin/sudo -S /usr/bin/apt-get install telnet -qy

這是因爲我們只是執行了大量的參數/bin/echo命令。 我認爲這是可能的實際運行整套使用bash命令的:

bash -c '/bin/echo myUserPass | /usr/bin/sudo -S /usr/bin/apt-get install telnet -qy' 

..但它不是,因爲bash -c '..'在Java中並不像它應該工作。它說-c'echo ...'腳本文件無法找到,所以我想它誤解了-c選項。 順便說一句,我從來沒有在單聲道C#這種問題。

這裏是整個片段:

package javaapplication1; 

import java.io.*; 

public class JavaApplication1 { 

    public static void main(String[] args) { 

     Process process; 
     String softwareToCheck = "telnet"; // Change here 

     try 
     {  
      if(!_softwareExists(softwareToCheck)) 
      { 
       System.out.println("Installing missing software.."); 
       process = Runtime.getRuntime().exec(new String[]{"/bin/bash","-c","'echo RIadminXsrv1 | sudo -S apt-get install telnet -qy'"}); 

       try 
       { 
        process.waitFor(); 
       } 
       catch(InterruptedException e) 
       { 
        System.out.println(e.getMessage()); 
       } 

       if(!_softwareExists(softwareToCheck)) 
       { 
        System.out.println("Software is still missing!"); 
       } 

      } 
      else 
      { 
       System.out.println("Software is installed!"); 
      } 
     } 
     catch(IOException e) 
     { 
      System.out.println(e.getMessage()); 
     }   
    } 

    private static boolean _softwareExists(String binaryName) throws IOException 
    { 
     String line; 
     ProcessBuilder builder; 
     BufferedReader reader; 
     Process process; 

     builder = new ProcessBuilder("/usr/bin/which", binaryName); 
     builder.redirectErrorStream(true); 
     process = builder.start(); 
     reader = new BufferedReader(new InputStreamReader(process.getInputStream())); 
     try 
     { 
      process.waitFor(); 
     } 
     catch(InterruptedException e) { 
      System.out.println(e.getMessage()); 
     } 

     while ((line = reader.readLine()) != null) 
     { 
      break; // Reads only the first line 
     } 

     return (line != null && !line.isEmpty()); 

    } 
} 
+0

我知道如何讀/寫過程,但你能告訴我如何檢查像ssh這樣的工具嗎? – Johnydep

+0

..但是_softwareExists(String binaryName)函數對於請求的第一部分是可以的。 – Salaros

+0

感謝您的答案 – Johnydep

2

在Ubuntu的/ Debian的你可以使用:

dpkg -s packagname 

,看看是否已安裝包。然後,您可以在應用程序中解析該命令的輸出。

+0

感謝,它工作得很好軟件包e.g火狐等,但像SSH公用事業,sshpass我總是得到相同的響應,它沒有安裝,儘管它們在系統中安裝? – Johnydep

0

,我不知道有任何工具,可以幫助,如果包管理工具不正確報告安裝的。可能是某些工具已安裝,但未更新數據庫。

檢查您的目標可執行文件是否存在於$ PATH和標準位置的任何目錄中可能很有用。

String pathString = System.getenv("PATH"); 
    String[] dirs= pathString.split(':'); 
    String[] exes= { "name1", "name2" ....}; 

    for (String exename : exes) { 
    for (String dirname : dirs) { 
      File exeFile=new File(dirname, exename); 
      //do some checks if file exists etc. 
    } 
    } 
相關問題