Raspberry Pi 4 Java and Apache Tomcat 8 Beginner’s Guide

Jahja Trifunovic
4 min readMar 20, 2021

Hey there! As I’ve started to learn Raspberry Pi recently, I promise this tutorial is going to be beginner-friendly. So, let’s learn together how to run Java web application with Apache Tomcat 8 on Raspberry Pi.

In this project I’ll be using Raspberry Pi 4, a laptop with Windows 10 OS and Remote Desktop Connection app for accessing to Raspberry.

Raspberry Pi 4
Raspberry Pi 4

First of all, we need to update the system. All these commands should be typed in terminal of Raspberry Pi.

sudo apt-get update 

After updating is done, we’ll install Apache Tomcat.

sudo apt-get install tomcat8

Now, to check if everything is installed properly we’ll need to find out IP address of your Raspberry Pi on the local network. In order to reveal it, just type in terminal command below:

ifconfig

Hopefully, if you visit 127.0.0.1:8080 on Raspberry Pi or it’s local network IP address (something like 192.168.8.107:8080 in my case) you can see this message:

Apache Tomcat 8 on Raspberry Pi 4
Apache Tomcat 8 on Raspberry Pi 4

Next step is to install Java on Raspberry Pi. The default Raspbian repositories have a fully-updated and compatible version of OpenJDK.

In a terminal window, enter the following:

sudo apt install default-jdk

You can verify the installation by checking the software version as follows:

java –version

This command will let you now what version of Java you have installed. In my case it’s 11.

Furthermore, it’s time to install IntelliJ IDEA. It can be used on Windows, Mac OS and Linux and can be downloaded from this link. I’m going to be using the Community edition of IntelliJ IDEA as it’s free and it’s what I use on Windows.

If you have successfully completed the previous tasks, you can visit Spring Initializr and generate demo project for Spring Boot framework.

Spring Initializr for Spring Boot
Spring Initializr for Spring Boot

It will help us to quickly start with Java based server app. During the initialization, you should pay attention on Java version. I chose Java 11 and dependencies, too. Make sure to choose Spring Web dependency. Above you can see what options I have selected. When you’re done with picking options, click GENERATE and download demo project.

After downloading is finished, extract demo file and open it with IntelliJ IDEA. Run IntelliJ IDEA, go to Open, navigate to demo and choose pom.xml.

Wait a few moments for project importing to be finished.

All you need to do now is to create a new class. This class will be responsible for giving response when our server’s IP is called.

The class must be marked with annotation @RestController as well as the method inside it with, for example, @GetMapping.

@GetMapping    
String myResposne(){
return "Java server hosted on Raspberry Pi";
}

In my case, I had to change the port, because the default Spring Boot app port is 8080 and it’s already in use on my Raspberry Pi. I’ve changed it to 8090 in application.properties file inside resources folder.

Application properties spring boot project
Application properties Spring Boot project

Then, if we run the project, it’ll turn our Raspberry Pi into Server. It’s available inside our local network.

Spring Boot server on Raspberry Pi 4
Spring Boot server on Raspberry Pi 4

However, you may ask why we can’t use 8080 port for our Spring Boot application. Well, the answer is very simple. It’s because Tomcat is already running on that port and it starts immediately after installing. You can stop it by executing a command bellow.

sudo update-rc.d tomcat8 disable

Yet, don’t forget to reboot Raspberry Pi after it.

Now you can comment port configuration in application.properties if you wish to use the port 8080.

And that’s it, we’ve made it! We’ve successfully installed Apache Tomcat 8 on Raspberry Pi. If you like this tutorial, please give it a clap and if you’re up for more Raspberry Pi projects, you can follow me on Medium. :)

--

--

Jahja Trifunovic
0 Followers

Android, Java, Kotlin, C++, Raspberry Pi, Arduino