run program packed in sh file

0

I downloaded jgit packed in sh file and ran it in mingw. I watched in process list that java process was started just with main class and parameters

"c:\Program Files\Java\jdk1.6.0_26\bin\java.exe" org.eclipse.jgit.pgm.Main diff master

I checked process current directory for unpacked main class, there was nothing! Where is main class located on file system?

misha nesterenko

Posted 2012-01-14T10:39:12.947

Reputation: 325

Answers

1

It sets the class path to the path of the .sh file in line 88 of the .sh file.

this_script=`which "$0" 2>/dev/null`
[ $? -gt 0 -a -f "$0" ] && this_script="$0"
cp=$this_script

[...]

CLASSPATH="$cp"
export CLASSPATH

There's no need to "unpack" the main class.


It uses a trick to combine a shell script and the actual JAR/ZIP data in a single file: It uses the fact that a ZIP file's central data structure is at its end. The zip/jar file reader simply about the "garbage data" shell script at the beginning.

Often the first thing in a zip file is a zip entry, which can be identified easily by its signature. But it is not necessarily the case that a zip file begins with a zip entry, and is not required by the zip specification.

As long as the shell script code exits before the actual zip/jar data starts, the shell doesn't care about the "garbage data" after the shell script part of the file either.

Daniel Beck

Posted 2012-01-14T10:39:12.947

Reputation: 98 421

Java process can directly read from sh file? – misha nesterenko – 2012-01-14T12:06:48.513

@mishanesterenko It's not an .sh file. It's a combination .sh/.jar file. See my edited answer. – Daniel Beck – 2012-01-14T12:19:22.220