title: java - using curl with Runtime.getRuntime().exec
body: <p>I want to use curl within a java program using Runtime.getRuntime().exec </p>
  
  <p>The full snippet is below yet what my problem boils down to is that I get an error response when I use the curl <em>command</em> in Runtime.getRuntime().exec(<em>command</em>) but when I System.out.println the <em>command</em>, copy and paste it to a shell and execute it there it works fine. </p>
  
  <pre><code>System.out.println(command);
  Runtime.getRuntime().exec(command);
  </code></pre>
  
  <p>What could cause this different outcome between runtime exec and executing the command in the shell. </p>
  
  <p>Thanks for any hints</p>
  
  <p>martin</p>
  
  <p>updates:</p>
  
  <ul>
  <li><p>The error response I get when using Runtime is: {"error":"unauthorized"}</p></li>
  <li><p>As I see from the commands that seems to be unclear. I dont get any Exception the curl command runs through but the json response is as posted above.</p></li>
  </ul>
  
  <hr>
  
  <pre><code>String APP_ID = "XXX";
  String REST_API_KEY = "YYY";
  
  String header = "-H \"X-Parse-Application-Id: " + APP_ID
          + "\" -H \"X-Parse-REST-API-Key: " + REST_API_KEY
          + "\" -H \"Content-Type: application/zip\" ";
  
  public void post2Parse(String fileName) {
  
      String outputString;
  
      String command = "curl -X POST " + header + "--data-binary '@"
              + fileName + "' https://api.parse.com/1/files/" + fileName;
  
      System.out.println(command);
  
      Process curlProc;
      try {
          curlProc = Runtime.getRuntime().exec(command);
  
          DataInputStream curlIn = new DataInputStream(
                  curlProc.getInputStream());
  
          while ((outputString = curlIn.readLine()) != null) {
              System.out.println(outputString);
          }
  
      } catch (IOException e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
      }
  }
  </code></pre>