title: Java Execute Shell Script Bundled within JAR
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/6603807/executing-a-shell-script-inside-a-jar-file-how-to-extract">Executing a shell script inside a jar file. How to extract?</a>  </p>
  </blockquote>
  
  
  
  <p>I have a shell script packaged into my JAR file within the 'engine' package.</p>
  
  <p>In my program I am running a shell command using Process and ProcessBuilder. This all works fine.</p>
  
  <p>If I specify the path to the shell script on my computer then the program works fine. If however I package the shell script into my JAR and access it like this:</p>
  
  <pre><code>scriptLocation = this.getClass().getResource("/engine/shell-script.sh").toString();
  </code></pre>
  
  <p>And run the program, then I get the following error:</p>
  
  <pre><code>java.io.IOException: Cannot run program "file:/Users/USERNAME/Desktop/Application-Name.jar!/engine/shell-script.sh": error=2, No such file or directory
      at java.lang.ProcessBuilder.start(ProcessBuilder.java:460)
      at java.lang.Runtime.exec(Runtime.java:593)
      at java.lang.Runtime.exec(Runtime.java:431)
      at java.lang.Runtime.exec(Runtime.java:328)
      at engine.Download$1.run(Download.java:39)
      at java.lang.Thread.run(Thread.java:680)
  Caused by: java.io.IOException: error=2, No such file or directory
      at java.lang.UNIXProcess.forkAndExec(Native Method)
      at java.lang.UNIXProcess.&lt;init&gt;(UNIXProcess.java:53)
      at java.lang.ProcessImpl.start(ProcessImpl.java:91)
      at java.lang.ProcessBuilder.start(ProcessBuilder.java:453)
      ... 5 more
  </code></pre>
  
  <p>I'm starting to think this cannot be done, I would very much like to provide this shell-script as part of the application, can it be done?</p>
  
  <p>Thanks in advance everyone.</p>
  
  <p><strong>======== UPDATE ==========</strong></p>
  
  <p>I eventually solved this problem. Shell cannot execute scripts within a JAR bundle, it's not about Java, but shell. I fixed this by doing the following:</p>
  
  <p>Java has a createTempFile() method (with option to delete the file once the application has been terminated), invoke this, and write the file within the JAR bundle that you want to access to this temp file. You then have the shell script on the local filesystem, and are able to execute the script.</p>