2011-03-14 67 views
2

上午運行於Mac OS的.pkg試圖從我的Java代碼運行的.mpkg應用:從Java代碼

 
public void runNewPkg(){ 

try { 

      String command = "sudo installer -pkg Snip.mpkg -target /Applications"; 
      Process p = Runtime.getRuntime().exec(command); 
      System.out.println(p.getErrorStream()); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 

而且正在以下錯誤,我的終端窗口掛..

[email protected] 
Password: 
Sumit-Ghoshs-iMac-3:downloads sumitghosh3$ Password: 
Password: 
-bash: **********: command not found 

Sumit-Ghoshs-iMac-3:downloads sumitghosh3$ 
  • 我想我還需要提供密碼才能從命令行運行pkg 您能告訴我我該怎麼做嗎?

回答

1

我實際上會嘗試編輯你的/ etc/sudoers文件來不提示輸入密碼。如果你使用NOPASSWD標籤,你應該可以做到這一點。一個示例項是:

sumitghosh3 ALL=(ALL) NOPASSWD: ALL 
2

您可以到須藤提供密碼:

echo "[email protected]" | sudo -S cal -y 2011 

上面的命令運行 'CAL -y 2011' 以root權限。

0

如果您想要提升特權的交互式解決方案,我已使用openscript來提升包裝的外殼腳本的特權。它是這樣的:

import java.io.File; 
import java.text.MessageFormat; 

/** 
* OsxExecutor.java 
*/ 
public class OsxExecutor { 

    private String error = null; 
    private String output = null; 

    /** 
    * Privileged script template format string. 
    * Format Arguments: 
    * <ul> 
    * <li> 0 = command 
    * <li> 1 = optional with clause 
    * </ul> 
    */ 
    private final static String APPLESCRIPT_TEMPLATE = 
     "osascript -e ''try''" 
     + " -e ''do shell script \"{0}\" {1}''" 
     + " -e ''return \"Success\"''" 
     + " -e ''on error the error_message number the error_number'' " 
     + " -e ''return \"Error: \" & error_message''" 
     + " -e ''end try'';"; 


    public void executeCommand(String command, boolean withPriviledge) { 
     String script = MessageFormat.format(APPLESCRIPT_TEMPLATE, 
              command, 
              withPriviledge 
               ? "with administrator privileges" 
               : ""); 
     File scriptFile = null; 
     try { 
      scriptFile = createTmpScript(script); 
      if (scriptFile == null) { 
       return; 
      } 
      // run script 
      Process p = Runtime.getRuntime().exec(scriptFile.getAbsolutePath()); 

      StreamReader outputReader = new StreamReader(p.getInputStream()); 
      outputReader.start(); 
      StreamReader errorReader = new StreamReader(p.getErrorStream()); 
      errorReader.start(); 

      int result = p.waitFor(); 

      this.output = outputReader.getString(); 
      if (result != 0) { 
       this.error = "Unable to run script " 
        + (withPriviledge ? "with administrator privileges" : "") 
        + "\n" + script + "\n" 
         + "Failed with exit code: " + result 
         + "\nError output: " + errorReader.getString(); 
       return; 
      } 
     } catch (Throwable e) { 
      this.error = "Unable to run script:\n" + script 
        + "\nScript execution " 
        + (withPriviledge ? " with administrator privileges" : "") 
        + " failed: " + e.getMessage(); 
     } finally { 
      if (scriptFile.exists()) { 
       scriptFile.delete(); 
      } 
     } 
    } 
} 

如果withPriviledge標誌爲true,密碼對話框將得到提升。未示出的是createTmpScript(),其在/tmpStreamReader中創建可執行文件,其延伸Thread並用於捕獲stdoutstderr流。