title: Running shell command from Spring WebApp
body: <p>I want to invoke mysqldump from a Spring based webapp and write the output to a file. I want to be able to provide a download link to the user to the file created on the server.</p>
  
  <p>I tried running the following:</p>
  
  <pre><code>Runtime.getRuntime().exec("/usr/bin/mysqldump -u &lt;username&gt; -p&lt;password&gt; &lt;db_name&gt; &gt;" + fileName);
  Runtime.getRuntime().exec("mysqldump -u &lt;username&gt; -p&lt;password&gt; db_name &gt;" + fileName);
  </code></pre>
  
  <p>I tried the following values of fileName.</p>
  
  <pre><code>fileName = "mysql.sql";
  fileName = "/tmp/mysql.sql";
  fileName = servletContext.getRealPath("") + "/mysql.sql";
  </code></pre>
  
  <p>But the command is not successful. process.exitValue() returns an error code of 2 or 6.</p>
  
  <pre><code>  ProcessBuilder pb = new ProcessBuilder("/usr/bin/mysqldump", " -u ", "&lt;username&gt;", " -p&lt;password&gt;", "&lt;db_name&gt;", " &gt; ", fileName);
    pb.redirectErrorStream(true); // equivalent of 2&gt;&amp;1
    Process p = pb.start();
    p.waitFor();
  </code></pre>
  
  <p>This does not help as well.</p>
  
  <p>Can someone tell me what is the best way to run such a command from the webapp? Where should the output file be created?
  What is wrong with the commands above?</p>
  
  <p>I need to be able to run this on different systems (Linux, Windows). The webapp runs on either system. I can abstract out the mysqldump path in a properties file if the need arises. But how do I make this work?</p>