Distributing a Java application for Linux while reaching the widest possible audience is complicated. Typically, the user has to make sure the JRE/SDK version and their environment are configured correctly.
When a Linux distribution changes the delivered JRE, this can be problematic for applications. Snapcraft ensures the correct JRE is shipped alongside the application at all times.
Snaps solve these problems and ensure the correct JRE is shipped alongside the application at all times.
Ready to get started? By the end of this guide, you’ll understand how to make a snap of your Java app that can be published in the Snap Store, showcasing it to millions of Linux users.
For a brief overview of the snap creation process, including how to install snapcraft and how it’s used, see Snapcraft overview. For a more comprehensive breakdown of the steps involved, take a look at Creating a snap.
Snaps are defined in a single YAML file placed in the root folder of your project. The following example shows the entire snapcraft.yaml file for an existing project, Freeplane. Don’t worry, we’ll break this down.
Using a few lines of yaml and the snapcraft tool, a Java application, it’s dependencies and the correct JRE can be packaged as a snap. We’ll break this down.
name: test-freeplane version: '1.6.10' summary: | Application for Mind Mapping, Knowledge and Project Management description: | Freeplane is a free and open source software application that supports thinking, sharing information and getting things done at work, in school and at home. The core of the software is tools for mind mapping (also known as concept mapping or information mapping) and using mapped information. confinement: devmode parts: test-freeplane: after: [desktop-glib-only] plugin: gradle source: . build: | export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64" gradle release -x test -x createGitTag install: | unzip DIST/freeplane_bin-*.zip -d \ $SNAPCRAFT_PART_INSTALL/ build-packages: - unzip - openjdk-8-jdk apps: test-freeplane: command: desktop-launch \ $SNAP/freeplane-1.6.10/freeplane.sh
snapcraft.yaml file starts with a small amount of human-readable metadata, which usually can be lifted from the GitHub description or project README.md. This data is used in the presentation of your app in the Snap Store.
name: test-freeplane version: '1.6.10' summary: | Application for Mind Mapping, Knowledge and Project Management description: | Freeplane is a free and open source software application that supports thinking, sharing information and getting things done at work, in school and at home. The core of the software is tools for mind mapping (also known as concept mapping or information mapping) and using mapped information. confinement: devmode
The next section describes the level of confinement applied to your app.
Snaps are containerised to ensure more predictable application behaviour and greater security. Unlike other container systems, the shape of this confinement can be changed through a set of interfaces. These are declarations that tell the system to give permission for a specific task, such as accessing a webcam or binding to a network port.
It’s best to start a snap with the confinement in warning mode, rather than strictly applied. This is indicated through the
devmode keyword. When a snap is in devmode, runtime confinement violations will be allowed but reported. These can be reviewed by running
Because devmode is only intended for development, snaps must be set to strict confinement before they can be published as “stable” in the Snap Store. Once an app is working well in devmode, you can review confinement violations, add appropriate interfaces, and switch to strict confinement.
Parts define how to build your app. Parts can be anything: programs, libraries, or other assets needed to create and run your application.
In this case we have two parts; the FreePlane source, and a remote
desktop-glib-only helper part. In other cases these can point to local directories, remote git repositories, or tarballs.
desktop-glib-only helper remote part will configure the runtime environment so that the application integrates well with the desktop environment. Other remote parts are available, and can be discovered via the
snapcraft search command.
The gradle plugin can build the application using standard parameters. In this case however we have overridden some gradle parameters (to disable testing, and prevent a new git tag being created), and set an appropriate
JAVA_HOME in a
build: script snippet. In the
install: script snippet we’re unpacking the built application into a directory which later gets incorporated into the final snap, defined by the
The build requires an appropriate JDK/JRE and the install step requires the addition of the
unzip command, so these are specified as
parts: test-freeplane: after: [desktop-glib-only] plugin: gradle source: . build: | export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64" gradle release -x test -x createGitTag install: | unzip DIST/freeplane_bin-*.zip -d \ $SNAPCRAFT_PART_INSTALL/ build-packages: - unzip - openjdk-8-jdk
For more details on Gradle-specific metadata, see The Gradle plugin.
Apps are the commands and services exposed to end users. If your command name matches the snap
name, users will be able run the command directly. If the names differ, then apps are prefixed with the snap
freeplane.command-name, for example). This is to avoid conflicting with apps defined by other installed snaps.
If you don’t want your command prefixed you can request an alias for it on the Snapcraft forum. These are set up automatically when your snap is installed from the Snap Store.
We have prefixed the FreePlane launch script with the
desktop-launch helper script, provided by the
desktop-glib-only remote part.
apps: test-freeplane: command: desktop-launch \ $SNAP/freeplane-1.6.10/freeplane.sh
If your application is intended to run as a service you simply add the line
daemon: simple after the command keyword. This will automatically keep the service running on install, update, and reboot.
You can download the example repository with the following command:
$ git clone https://github.com/snapcraft-docs/freeplane
After you’ve created the snapcraft.yaml, you can build the snap by simply executing the snapcraft command in the project directory:
$ cd freeplane $ snapcraft
The resulting snap can be installed locally. This requires the
--dangerous flag because the snap is not signed by the Snap Store. The
--devmode flag acknowledges that you are installing an unconfined application:
$ sudo snap install freeplane_*.snap --devmode --dangerous
You can then try it out:
Removing the snap is simple too:
$ sudo snap remove freeplane
To share your snaps you need to publish them in the Snap Store. First, create an account on the dashboard. Here you can customise how your snaps are presented, review your uploads and control publishing.
You’ll need to choose a unique “developer namespace” as part of the account creation process. This name will be visible by users and associated with your published snaps.
Make sure the
snapcraft command is authenticated using the email address attached to your Snap Store account:
$ snapcraft login
You can publish your own version of a snap, provided you do so under a name you have rights to. You can register a name on dashboard.snapcraft.io, or by running the following command:
$ snapcraft register myjavasnap
Be sure to update the
name: in your
snapcraft.yaml to match this registered name, then run
Use snapcraft to push the snap to the Snap Store.
$ snapcraft push --release=edge myjavasnap_*.snap
If you’re happy with the result, you can commit the snapcraft.yaml to your GitHub repo and turn on automatic builds so any further commits automatically get released to edge, without requiring you to manually build locally.
Congratulations! You’ve just built and published your first Go snap. For a more in-depth overview of the snap building process, see Creating a snap.