DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How to Identify the Underlying Causes of Connection Timeout Errors for MongoDB With Java
  • How To Connect a Heroku Java App to a Cloud-Native Database
  • How To Build a Multi-Zone Java App in Days With Vaadin, YugabyteDB, and Heroku
  • How Java Apps Litter Beyond the Heap

Trending

  • Java in a Container: Efficient Development and Deployment With Docker
  • The Agent Protocol Stack: MCP vs. A2A vs. AG-UI
  • Liquibase: Database Change Management and Automated Deployments
  • How AI Coding Assistants Are Changing Developer Flow
  1. DZone
  2. Coding
  3. Frameworks
  4. Run Your Java App as a Service on Ubuntu

Run Your Java App as a Service on Ubuntu

Bring your JAR file to Ubuntu as a service using this example service wrapper. See how to make it work, including automatic starts and logging tips.

By 
Muhammad Sarwar user avatar
Muhammad Sarwar
·
Updated Oct. 20, 17 · Tutorial
Likes (52)
Comment
Save
Tweet
Share
227.3K Views

Join the DZone community and get the full member experience.

Join For Free

Say you have a JAR file and you need to run it as a service. Additionally, you want it to start automatically if/when system restarts.

Ubuntu has a built-in mechanism to create custom services, enabling them to get started at system boot time and start/stop them as a service. In this post, I am going to share a simple and elegant way to create a service wrapper for your JAR file so you can run it as a service. Here we go.

Step 1: Create a Service

sudo vim /etc/systemd/system/my-webapp.service


Copy/paste the following into the file /etc/systemd/system/my-webapp.service:

[Unit]
Description=My Webapp Java REST Service
[Service]
User=ubuntu
# The configuration file application.properties should be here:

#change this to your workspace
WorkingDirectory=/home/ubuntu/workspace

#path to executable. 
#executable is a bash script which calls jar file
ExecStart=/home/ubuntu/workspace/my-webapp

SuccessExitStatus=143
TimeoutStopSec=10
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target


Step 2: Create a Bash Script to Call Your Service

Here’s the bash script that calls your JAR file: my-webapp

#!/bin/sh
sudo /usr/bin/java -jar my-webapp-1.0-SNAPSHOT.jar server config.yml


Don't forget to give your script execute permission: sudo chmod u+x my-webapp

Step 3: Start the Service

sudo systemctl daemon-reload
sudo systemctl enable my-webapp.service
sudo systemctl start my-webapp
sudo systemctl status my-webapp


Step 4: Set Up Logging

First, run: sudo journalctl --unit=my-webapp . See real-time logs by using the -f option.

If you want to trim them, use -n <# of lines> to view the specified number of lines of the log:

sudo journalctl -f -n 1000 -u my-webapp


Tail the live log using the -f option:

sudo journalctl -f -u my-webapp


Stop the service by using:

sudo systemctl stop my-webapp


That's it! Enjoy and show your support if you like it. Thanks!

ubuntu app Java (programming language)

Published at DZone with permission of Muhammad Sarwar. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How to Identify the Underlying Causes of Connection Timeout Errors for MongoDB With Java
  • How To Connect a Heroku Java App to a Cloud-Native Database
  • How To Build a Multi-Zone Java App in Days With Vaadin, YugabyteDB, and Heroku
  • How Java Apps Litter Beyond the Heap

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook