title: Run shell command from priv-app application
body: <p>I am trying to run shell commands from a system/priv-app I have added to the AOSP (7.1.1) </p>
  
  <p>The command I am trying to run is: <strong>ip link add dev can0 type can</strong> to switch on the can bus.</p>
  
  <p>I have build the image as both <strong>-eng</strong> &amp; <strong>-userdebug</strong> releases.
  The command runs fine in the adb shell and successfully switches on the CAN bus as expected.</p>
  
  <p>My problem is that I get the following error:</p>
  
  <blockquote>
    <p>Cannot run program "su": error=13, Permission denied</p>
  </blockquote>
  
  <p>When I try the following code within the system privileged java app:</p>
  
  <pre><code>//ArrayList&lt;String&gt; commands is passed into the method
  try {
    if (null != commands &amp;&amp; commands.size() &gt; 0) {
       Process suProcess = Runtime.getRuntime().exec("su");
       DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
       for (String currCommand : commands) {
          os.writeBytes(currCommand + "\n");
          os.flush();
       }
       os.writeBytes("exit\n");
       os.flush();
       BufferedReader stderr = new BufferedReader(new InputStreamReader(suProcess.getErrorStream()));
       String line = "";
       String errString = "";
       while ((line = stderr.readLine()) != null) errString += line + "\n";
       suProcess.waitFor();
       if (suProcess.exitValue() != 0)
          throw new Exception(errString);
   } //Handle exception
  </code></pre>