2011-03-14 6 views
2

essaie d'exécuter une application .mpkg de mon code java:Exécution .pkg sur MAC OS à partir du code 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(); 
     } 
    } 

et je obtiens l'erreur suivante et ma fenêtre de terminal .. se bloque

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

Sumit-Ghoshs-iMac-3:downloads sumitghosh3$ 
  • Je pense que j'ai besoin de fournir le mot de passe pour exécuter le pkg depuis la ligne de commande Pourriez-vous me dire comment je peux faire ça?

Répondre

1

Je voudrais réellement essayer de modifier votre fichier/etc/sudoers pour ne pas demander un mot de passe. Si vous utilisez le tag NOPASSWD, vous devriez être capable de le faire. Un exemple d'entrée serait:

sumitghosh3 ALL=(ALL) NOPASSWD: ALL 
2

Vous pouvez fournir le mot de passe sudo:

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

La commande ci-dessus de 2011 -y cal »fonctionne avec les permissions root.

0

Si vous voulez une solution interactive pour l'élévation des privilèges, j'ai utilisé openscript pour élever le privilège d'un script shell enveloppé. Il va quelque chose comme ceci:

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(); 
      } 
     } 
    } 
} 

Si withPriviledge drapeau est vrai, une boîte de dialogue de mot de passe sera élevé. Non illustré est createTmpScript() qui crée un fichier exécutable dans /tmp et StreamReader qui s'étend Thread et est utilisé pour capturer les flux stdout et stderr.

Questions connexes