title: How to run sudo command for OS X in java
body: <p>I am working on app in javafx , I am trying open an application using a command in terminal, I am running the command using my java code my command have some variable it's have path of my installer file which will not always be same because file name can be different as the builds are updated.<br>
  here is a sample as how I am running the command it's not the exact command which I am running but the command format is same.</p>
  
  <pre><code>Process process = Runtime.getRuntime().exec("echo password | sudo -S open -a safari");
      String line;
      BufferedReader input = new BufferedReader(new InputStreamReader(pb.getInputStream()));
      while ((line = input.readLine()) != null) {
          System.out.println(line);
      }
      input.close();
  </code></pre>
  
  <p>The process is not giving any output it stops there and nothing happens.
  The same command I have tried from terminal and it works fine.</p>
  
  <p>I have tried the things mention in this link </p>
  
  <p><a href="https://stackoverflow.com/questions/18708087/how-to-execute-bash-command-with-sudo-privileges-in-java">How to execute bash command with sudo privileges in Java?</a></p>
  
  <p>but it also didn't worked.</p>
  
  <p>I am also running command like "chmod +x" from my java code these commands runs fine.
  my original command looks like this:-</p>
  
  <pre><code>runCommand = "echo" + " " + password + "| sudo -S " + "\"" + a.getAbsolutePath() + "\"" + " --deploymentFile="
                              + "\"" + b.getAbsolutePath() + "\"";
  </code></pre>
  
  <p>where a.getAbsolutePath() is the path of the installer file and b.getAbsolutePath() is the path of the deployment file we used to install the application.</p>
  
  <pre><code>pb.getInputStream()
  </code></pre>
  
  <p>prints the command and when I copy and paste it is terminal it's runs fine.</p>
  
  <pre><code>pb.getErrorStream()
  </code></pre>
  
  <p>doesn't give anything.</p>
  
  <p>I have tried running </p>
  
  <pre><code>String[] cmd = {"/bin/bash","-c","echo tester| sudo -S ","\"",a.getAbsolutePath(),"\"","\""," --deploymentFile=","\"",b.getAbsolutePath()};
  </code></pre>
  
  <p>and </p>
  
  <pre><code>String[] cmd = {"/bin/bash","-c","echo tester| sudo -S",a.getAbsolutePath(),"--deploymentFile=","\"",b.getAbsolutePath()};
  </code></pre>
  
  <p>also 
  here I got following error </p>
  
  <pre><code>getErrorStreamusage: sudo -h | -K | -k | -L | -V
  getErrorStreamusage: sudo -v [-AknS] [-g groupname|#gid] [-p prompt] [-u user name|#uid]
  getErrorStreamusage: sudo -l[l] [-AknS] [-g groupname|#gid] [-p prompt] [-U user name] [-u
  getErrorStream            user name|#uid] [-g groupname|#gid] [command]
  getErrorStreamusage: sudo [-AbEHknPS] [-C fd] [-g groupname|#gid] [-p prompt] [-u user
  getErrorStream            name|#uid] [-g groupname|#gid] [VAR=value] [-i|-s] [&lt;command&gt;]
  getErrorStreamusage: sudo -e [-AknS] [-C fd] [-g groupname|#gid] [-p prompt] [-u user
  getErrorStream            name|#uid] file ...
  </code></pre>