Create a Debian package from an executable jar

We’ll use our simple game Cave Fizzer as an example again. It’s a libGDX project and we can simply export the desktop sub-project as a runnable jar. Once we have done so the challenge is to generate a .deb that we can install on Debian or Ubuntu.

A useful guide to build a Debian installer can be found here: http://blog.pryds.eu/2013/02/package-java-apps-for-ubuntu-and-debian.html. It covers all the information needed to get the job done. Please refer to it for further explanations. I don’t want to be redundant here.

Although I encourage you to study the subject in order to understand what is going on, we’ll take the pragmatic approach here and apply a shell script to quickly build the package.

So export your runnable jar. From now on we’ll be working outside Eclipse (or any other IDE). Create a directory, for the Cave Fizzer example:

mkdir debcavefizzer
cd debcavefizzer

Copy your jar into this directory. You’ll need several more configuration files, ending up like this:

~/debcavefizzer$ ls -1
cavefizzer
cavefizzer.desktop
cavefizzericon.svg
CaveFizzer.jar
changelog
control
copyright
packagedeb.sh

The first one is a shell script to start the Java application. A symbolic link to the jar file would do as well (as long as jarwrapper or similar functionality has been installed).

cavefizzer:

#!/bin/sh
java -jar /usr/share/cavefizzer/CaveFizzer.jar

The desktop file is needed to have a categorized launcher. In this example it looks like this:

cavefizzer.desktop:

[Desktop Entry]
Encoding=UTF-8
Name=Cave Fizzer
Comment=Cave Fizzer Game
Exec=/usr/games/cavefizzer
Icon=/usr/share/cavefizzer/cavefizzericon.svg
Terminal=false
Type=Application
Categories=Game
StartupNotify=true

Vector graphics are scalable, so we use a 32 * 32 svg here as an icon, made with Inkscape.

Debian requires a changelog. We’ll generate it with our shell script later.

The control file contains the metadata about our package, including (most importantly) its dependencies.

control:

Section: games
Priority: optional
Architecture: all
Maintainer: Serge Helfrich 
Description: How deep can you penetrate our cave system?
 Meet the Fizzer in his exploration of the underground world.
 * Travel as far as you can.
 * Do not hit the rocks.
 * Impress your friends on Google Play Games.
Depends: openjdk-8-jre | openjdk-7-jre | openjdk-6-jre | oracle-java8-installer | oracle-java7-installer | oracle-java6-installer | jarwrapper

Debian requires a copyright notice. For now:

copyright:

This package was debianized by Serge Helfrich helfrich@xs4all.nl on
Sun Oct 19 13:51:31 CEST 2014.

It was downloaded from http://pygmalion.nitri.de/cavefizzer/CaveFizzer.jar

Copyright: (c) 2014 Serge Helfrich

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with Debian system; see the file /usr/doc/copyright/GPL. If not,
write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
MA 02110-1301, USA.

Finally we write a shell script to wrap it all up and deliver a .deb package:

packagedeb.sh:

#!/bin/sh
 
PACKAGE_NAME="cavefizzer"
PACKAGE_VERSION="0.1.1"
SOURCE_DIR=$PWD
TEMP_DIR="/tmp"
 
mkdir -p $TEMP_DIR/debian/DEBIAN
mkdir -p $TEMP_DIR/debian/lib
mkdir -p $TEMP_DIR/debian/usr/games
mkdir -p $TEMP_DIR/debian/usr/share/applications
mkdir -p $TEMP_DIR/debian/usr/share/$PACKAGE_NAME
mkdir -p $TEMP_DIR/debian/usr/share/doc/$PACKAGE_NAME
mkdir -p $TEMP_DIR/debian/usr/share/common-licenses/$PACKAGE_NAME
 
echo "Package: $PACKAGE_NAME" > $TEMP_DIR/debian/DEBIAN/control
echo "Version: $PACKAGE_VERSION" >> $TEMP_DIR/debian/DEBIAN/control
cat control >> $TEMP_DIR/debian/DEBIAN/control
 
cp *.desktop $TEMP_DIR/debian/usr/share/applications/
cp copyright $TEMP_DIR/debian/usr/share/common-licenses/$PACKAGE_NAME/ # results in no copyright warning
#cp copyright $TEMP_DIR/debian/usr/share/doc/$PACKAGE_NAME/ # results in obsolete location warning
 
cp *.jar $TEMP_DIR/debian/usr/share/$PACKAGE_NAME/
cp $PACKAGE_NAME $TEMP_DIR/debian/usr/games/
 
echo "$PACKAGE_NAME ($PACKAGE_VERSION) trusty; urgency=low" > changelog
echo "  * Rebuild" >> changelog
echo " -- Serge Helfrich <helfrich@xs4all.nl>  `date -R`" >> changelog
gzip -9c changelog > $TEMP_DIR/debian/usr/share/doc/$PACKAGE_NAME/changelog.gz
 
cp *.svg $TEMP_DIR/debian/usr/share/$PACKAGE_NAME/
chmod 0664 $TEMP_DIR/debian/usr/share/$PACKAGE_NAME/*svg
 
PACKAGE_SIZE=`du -bs $TEMP_DIR/debian | cut -f 1`
PACKAGE_SIZE=$((PACKAGE_SIZE/1024))
echo "Installed-Size: $PACKAGE_SIZE" >> $TEMP_DIR/debian/DEBIAN/control
 
chown -R root $TEMP_DIR/debian/
chgrp -R root $TEMP_DIR/debian/
 
cd $TEMP_DIR/
dpkg --build debian
mv debian.deb $SOURCE_DIR/$PACKAGE_NAME-$PACKAGE_VERSION.deb
rm -r $TEMP_DIR/debian

Run the script as root:

sudo ./packagedeb.sh

Run lintian on the result and make adjustments where necessary.

lintian -i cavefizzer-0.1.1.deb

One thought on “Create a Debian package from an executable jar

Leave a Reply