title: Java Runtime.exec woes on Linux
body: <p>I am working on a program in Java designed to be used on a Linux environment that creates a new Java process that runs another Java class, but I am having trouble with it. I have finally fixed all my issues up to this. Invoking</p>
  <pre><code>Runtime.getRuntime().exec(new String[] { &quot;/bin/bash&quot;, &quot;-c&quot;, &quot;'java -classpath /home/kevin/workspace/Misc/bin HelloWorld'&quot; })
  </code></pre>
  <p>in my Java program returns</p>
  <pre><code>/bin/bash: /usr/lib/jvm/java-6-openjdk/jre/bin/java -classpath /home/kevin/workspace/Misc/bin HelloWorld: No such file or directory
  </code></pre>
  <p>in either stdout/stderr. If I try</p>
  <pre><code>Runtime.getRuntime().exec(new String[] { &quot;/bin/bash -c 'java -classpath /home/kevin/workspace/Misc/bin HelloWorld'&quot; })
  </code></pre>
  <p>I get a Java exception</p>
  <pre><code>Cannot run program &quot;/bin/bash -c 'java -classpath /home/kevin/workspace/Misc/bin HelloWorld'&quot;: java.io.IOException: error=2, No such file or directory
       ...
  Caused by: java.io.IOException: java.io.IOException: error=2, No such file or directory
  </code></pre>
  <p>And finally, using a simple</p>
  <pre><code>Runtime.getRuntime().exec(&quot;/bin/bash -c 'java -classpath /home/kevin/workspace/Misc/bin HelloWorld'&quot;)
  </code></pre>
  <p>gives me a</p>
  <pre><code>-classpath: -c: line 0: unexpected EOF while looking for matching `''
  -classpath: -c: line 1: syntax error: unexpected end of file
  </code></pre>
  <p>from stdout/stderr.</p>
  <p>Meanwhile, creating a new one line .sh file (and assigning proper permissions) with only this (no #!/bin/bash at the top of the file)</p>
  <pre><code>/bin/bash -c 'java -classpath /home/kevin/workspace/Misc/bin HelloWorld'
  </code></pre>
  <p>gives the correct output with no errors.</p>
  <p>I understand that the usage Runtime.exec is quite complicated to perfect, and I already solved some big problems I got from it before, but this problem just plain puzzles me (such as Runtime.exec's use of StringTokenizer screwing up any commands that have spaces in them, which is why I invoked the overload that accepts String arrays). However, I'm still getting problems with it and I don't understand why.</p>