title: java Runtime.exec to run shell script
body: <p>I am using Runtime.getRuntime().exec() to run a shell script from Java code. The code works fine when I pass the parameter as string</p>
  <pre><code>      Runtime.getRuntime().exec(&quot;sh test.sh&quot;)
  </code></pre>
  <p>Since I have to pass additional arguments which are paths with spaces, so I replaced String with String array.</p>
  <pre><code>      String[] cmd = {&quot;sh test.sh&quot;, &quot;/Path/to my/resource file&quot;};
        Runtime.getRuntime().exec(cmd)
  </code></pre>
  <p>I also tried with</p>
  <pre><code>      String[] cmd = {&quot;sh test.sh&quot;};
        Runtime.getRuntime().exec(cmd)
  </code></pre>
  <p>But neither of them worked. It's throwing an exception:</p>
  <pre><code>   java.io.IOException: Cannot run program &quot;sh test.sh&quot;:
     java.io.IOException: error=2, No such file or directory
  </code></pre>
  <p>Why is the same script file when passed as String worked and when used with String array is throwing exception? How can I make this work with string array as argument to <code>Runtime.exec()</code>?</p>