Jpackage : Overview of one of Java 14's hidden gems
Tags: java, development
Published: 2020-04-09 15:40:10

---
title: Jpackage : Overview of one of Java 14's hidden gems
published: true
date: 2020-04-02 19:29:52 UTC
tags: java,development
canonical_url: https://lengrand.fr/jpackage-an-overview-of-one-of-java-14s-hidden-gems/
---

 ![Jpackage : Overview of one of Java 14's hidden gems](https://images.unsplash.com/photo-1524685794168-52985e79c1f8?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjExNzczfQ)

Typically, Java applications are deployed packaged as jar files, or war files for web applications. Newly, Java 14 comes with a new `jpackage` command that allows us to **create system applications** (.msi, .pkg or .deb files).

Let's have a quick tour of the problems it can solve. For a complete description, you can have a look at the [official JDK enhancement proposal](https://openjdk.java.net/jeps/343). As an experiment, let's create installers of one of the most famous Java piece of software out there : **[The Minecraft server](https://www.minecraft.net/fr-fr/download/server/)!**

## Checking that we have the correct version of Java

You'll need the latest Java 14 to enjoy the show. Luckily, **Intellij** allows to easily and painlessly [install new JDKs](https://www.jetbrains.com/help/idea/sdk.html#define-sdk). Let's make use of it!

We create a new empty project, and download the [OpenJDK 14](https://openjdk.java.net/).

<!--kg-card-begin: image-->

![Jpackage : Overview of one of Java 14's hidden gems](https://lengrand.fr/content/images/2020/04/new-project.gif)<figcaption>Creating a new project with SDK in Intellij</figcaption>

<!--kg-card-end: image-->

Once this is done, we can check that our java version is valid, and that we have access to `jpackage` as well.

<!--kg-card-begin: image-->

![Jpackage : Overview of one of Java 14's hidden gems](https://lengrand.fr/content/images/2020/04/java-version.gif)<figcaption>Checking Java and jpackage are available</figcaption>

<!--kg-card-end: image-->

All good so far. The last thing we need is the Minecraft server itself. We can [download it here from the folks at Mojang](https://www.minecraft.net/fr-fr/download/server/), and place it at the root of our project.

## Creating the installer

Alright, we have everything ready. We'll make a first barebone attempt.

<!--kg-card-begin: markdown-->
```
$ jpackage --name minecraft-jpackage --main-jar server.jar --input .

```
<!--kg-card-end: markdown-->

That's it! What did we do here? We indicated that

- Our main jar file was `server.jar` (It already contains a Main class and the proper [Manifest](https://docs.oracle.com/javase/tutorial/deployment/jar/appman.html) so we are good to go.)
- It was located inside the current folder (using the `--input` option).
- We wanted an executable called `minecraft-jpackage`. This is also the folder in which our application will install itself (`C:\Program Files\minecraft-jpackage` in this case).

We let the command run, and quickly end up with a `minecraft-jpackage-1.0.exe` file in our repository.

> On my machine, the installer is just over 80Mb, for an original server of about 35Mb. Not too bad given that we embed a whole JRE as well. The installed version is about 160Mb.

## The grand finale

When running the executable, we see a simple (and native looking) installation menu. It installs as expected (you might get a warning because the installation wasn't signed, which is normal).

_Now, before enjoying our server, we have to[go around a little quirk of Minecraft](https://minecraft.gamepedia.com/Tutorials/Setting_up_a_server)._

- _Run the executable file once as administrator._
- _In the installation folder, a `eula.txt` file has been created_
- _As administrator, change the value inside to `eula=true` (meaning you accept the EULA and save the file._

Launch the application a second time! We can enjoy our Minecraft server!

<!--kg-card-begin: image-->

![Jpackage : Overview of one of Java 14's hidden gems](https://lengrand.fr/content/images/2020/04/Capture3.PNG)<figcaption>Fully functioning Minecraft server</figcaption>

<!--kg-card-end: image-->
## Diving further

Now that we have a working installer, let's try out some more options!

<!--kg-card-begin: markdown-->
```
$ jpackage --name minecraft-jpackage --main-jar server.jar --input . --java-options -Xmx1024M --java-options -Xms1024M --win-dir-chooser --win-shortcut

```
<!--kg-card-end: markdown-->

With a few extra options, we can get a nicer experience :

- The [recommended java settings for running Minecraft](https://www.minecraft.net/fr-fr/download/server/)
- A shortcut to run the application from the Desktop
- A dialog to choose our installation folder. This is very nice in our case because it avoids having to run Minecraft as administrator as required above.
<!--kg-card-begin: image-->

![Jpackage : Overview of one of Java 14's hidden gems](https://lengrand.fr/content/images/2020/04/Capture5.PNG)<figcaption>Minecraft's installation dialog</figcaption>

<!--kg-card-end: image-->
## Conclusion

A lot of the articles around Java 14 focus on [records](https://openjdk.java.net/jeps/359)or the new [switch expressions](https://openjdk.java.net/jeps/361). But the `jpackage` is, as we have seen it, a small nugget that can have very practical uses! With a simple (CLI) interface, support for all OSes, it can be a perfect companion in some use cases.