Mini-Project: Deploying Java Application with Tomcat

In this session, I’ll show you how to deploy ‘Hello World’ Java application with tomcat. Before we heading to the main course, it’s good to know the requirements.

Apache Tomcat

Apache Tomcat is an open-source implementation of the Java Servlet, JavaServer Pages, Java Expression Language, and WebSocket technologies. Tomcat provides a “pure Java” HTTP web server environment in which Java code can run.

Later, I’ll use Tomcat 9 that requires Java version installed on the system. We’ll install OpenJDK 11, the open-source implementation of the Java Platform.


Maven is a build automation tool used primarily for Java projects. Maven can also be used to build and manage projects written in C#, Ruby, Scala, and other languages. The Maven project is hosted by the Apache Software Foundation, where it was formerly part of the Jakarta Project.

Step 1 — Setting Up Environment

- Install Java 11

# apt install openjdk-11-jdk

- Check Java version

# java -version
openjdk version "11.0.8" 2020-07-14

- Download Tomcat from source

At the time of writing, the latest Tomcat version is 9.0.37. Before continuing with the next step, check the Tomcat 9 download page to see if a newer version is available.

# wget

- Extract the file

# mkdir /etc/tomcat

Once the download is complete, make a directory named tomcat under /etc and then extract the tar file to the /etc/tomcat directory.

# tar -xf apache-tomcat-9.0.37.tar.gz -C /etc/tomcat/

- Make the script executable

# sh -c ‘chmod +x /etc/tomcat/bin/*.sh’

- Make the service run as a service

# nano /etc/systemd/system/tomcat.service

Instead of using the shell scripts to start and stop the Tomcat server, we’ll set it to run as a service. Paste the following script:

Description=Tomcat 9 servlet container



Environment=" -Djava.awt.headless=true"

Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"



- Restart the daemon

Notify systemd that a new unit file exists.

# systemctl daemon-reload

- Start the Tomcat

# systemctl start tomcat

If it running properly, you can check on your web by submitting your VM’s IP. By default, Tomcat running on port 8080.

Step 2 — Create a Java Application

- Install a Java build tool

As mentioned before, we will use Maven as the Java build tool. So, make sure Maven installed on your VM. If you don’t have it already, you can easily install it using this line.

# apt install maven

- Check Maven version

# mvn -version
Apache Maven 3.6.3

- Generate project

Maven has a feature that can generate an initial maven project with a folder structured. Just paste this following line:

# mvn archetype:generate -DartifactId=java-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

The folder structure should be like this:

├── pom.xml
└── src
├── main
│ └── java
│ └── com
│ └── app
│ └── example
│ └──
└── test
└── java
└── com
└── app
└── example

- Configure the application

# nano pom.xml

POM is the fundamental unit of work in Maven. It is an XML file that contains information about the project and configuration details used by Maven to build the project. It contains default values for most projects. So, we’ll change the pom.xml with this buck of the script:

- Create the application

# nano src/main/java/com/app/example/

Change the contents of the default java application with this code:

- Build the application

Build the application using this command that will download application dependencies, compile, test, and package it as a distributed application.

# mvn package

Step 2 — Deploy the Application

- Copy application to Tomcat directory

The result of the previous step is a distributed application with .war extension stored in the target folder under java-app. That .war application will later be deployed on the Tomcat service.

# cp java-app/target/hello-world-1.war /etc/tomcat/webapps/

Once we’ve done with copying the app, restart Tomcat service.

# systemctl restart tomcat

- Check the service

You should now be able to go to your VM’s IP at port 8080 with location /hello-world-1 in your web browser and see your app running.





Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store