Possible Duplicate:
Executing a shell script inside a jar file. How to extract?
I have a shell script packaged into my JAR file within the 'engine' package.
In my program I am running a shell command using Process and ProcessBuilder. This all works fine.
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:
scriptLocation = this.getClass().getResource("/engine/shell-script.sh").toString();
And run the program, then I get the following error:
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.<init>(UNIXProcess.java:53)
at java.lang.ProcessImpl.start(ProcessImpl.java:91)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:453)
... 5 more
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?
Thanks in advance everyone.
======== UPDATE ==========
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:
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.