Shell script with jar file at the end Shell script with jar file at the end shell shell

Shell script with jar file at the end


Try by yourself the following commands. Start creating a normal jar file with any content, or use someone you have. I will name it "myjar.jar"

Next, create a file "hello.sh" with content:

#!/bin/bashexec echo hello

now, add this file at start of a new jar file:

cat hello.sh myjar.jar > mytrick.jarchmod 700 mytrick.jar

And finally, the interesting part, type:

./mytrick.jarjar -tf mytrick.jarunzip mytrick.jar

in other words, usually jar/unzip skips any content until their own header. Moreover, a shell script "ends" in a line who call "exec" (because shell interpreter is replace at this point by the command in the exec line).

However, this trick is based in a behaviour of jar/unzip probably out of standards. Note, by example, that this statement fails (has no effects):

jar -xf mytrick.jar


If the file after extracting the tar file is start-superbeam.sh try doing chmod +x start-superbeam.sh && ./start-superbeam.sh or /bin/sh ./start-superbeam.sh.

If the program has arguments, supply them after at the end. It will run java on that the superbeam.sh which as a jar file at the end.

If you need special java parameters set such as for memory size, you would set them in the environment variable java_args.

As for what's going on this is a shell script with a jar file at the end of it after the exit. To quote from ReallyExecutable Jars:

There has long been a hack known in some circles, but not widely known, to make jars really executable, in the chmod +x sense. The hack takes advantage of the fact that jar files are zip files, and zip files allow arbitrary cruft to be prepended to the zip file itself (this is how self-extracting zip files work).

As for how to create, see the accepted answer or the link.