Sunday, October 5, 2008

launch4l

I wanted to create a self-launching UNIX jar file, similar to what launch4j does for Windows. There are a couple of solutions out there, but none worked well for me. So I wrote my own. Here it is (formatted via http://formatmysourcecode.blogspot.com/):

#! /bin/bash

path=$1
jarname=${path##.*/}
basename=${jarname%%.jar}

## want substitutions this time as we need jarname expanded
cat - << ONE > $basename
#! /bin/bash

trap 'rm -f /tmp/$jarname' ERR
rm -f /tmp/$jarname
ONE

## but not here
cat - << "TWO" >> $basename
UUDECODE=`which uudecode 2> /dev/null`
if [ "$UUDECODE" = "" ]
then
UUDECODE=`which gmime-uudecode 2> /dev/null`
if [ "$UUDECODE" = "" ]
then
echo "No uudecode command found in $PATH, exiting"
exit 1
fi
fi

TWO

## but here again
echo '$UUDECODE -o /tmp/'$jarname' << "ENDOFJAR"' >> $basename

UUENCODE=`which uuencode 2> /dev/null`
if [ "$UUENCODE" = "" ]
then
UUENCODE=`which gmime-uuencode 2> /dev/null`
if [ "$UUENCODE" = "" ]
then
echo "No uuencode command found in $PATH, exiting"
exit 1
fi
fi

$UUENCODE $path /tmp/$jarname >> $basename

cat - << THREE >> $basename
ENDOFJAR

java -jar /tmp/$jarname \$*
rm -f /tmp/$jarname
THREE

chmod u+x $basename


No comments: