How To Run a Java Application on Linux using Systemd

Systemd is a system manager and the new Linux init system. This guide shows how to start a Java application with the systemctl command.

Create Java Application User

For security purposes, the application should be run as an unprivileged user (i.e. not root). We will create a new user and group that will run the application service.

First, create a new jvmapp group:

$ sudo groupadd -r jvmapp

Next, create a new jvmapp user. This user should be a member of the former created jvmapp group, with a home directory of where the location of the application is (e.g. /opt/application), and with a shell of /bin/false (so nobody is able to log into the account)

$ sudo useradd -r -g jvmapp -s /bin/false -d /opt/application jvmapp

Now, your jvmapp user is set up.

Create a systemd Service File

As the user root, create a application.service file in the /etc/systemd/system directory.

$ vi /etc/systemd/system/application.service

Write the following content inside the file.

Description=Java Application Service

ExecStart=/path/to/java -jar /path/to/application.jar



Start the application as a service

After saving the file, reload the systemd daemon so that it knows about the new service.

$ systemctl daemon-reload

Start the Application service by typing

$ systemctl start application

Show the status, if the service is started

$ systemctl status application

Now the service should start by each start of the system.

Leave a Reply

Your email address will not be published. Required fields are marked *