Deploy Java (JAX-RS) REST API to tomcat server using CLI without Eclipse IDE.

Shihara Dilshan
5 min readMay 5, 2021

Hello everyone I hope you all are doing great. Today I am going to talk about how you can deploy and test your JAX-RS rest API to a tomcat server using command line. But keep in mind you will need Eclipse at some point(To create dynamic web application and export into a WAR file).

Some of you may ask Why do I need to do this?

Answer for that question is simple, Let’s say you are using microservice architecture to create your backend. And you have chosen Java (JAX-RS) as your stack. For that you will need eclipse. That point you will have to run multiple services at the same time and also you will have to open some applications like postman, google chrome and multiple database servers. So for the people’s who only have 4GB or lesser ram will definitely have some hard time with all of those applications 🤪. So why to run eclipse when you can run your API tomcat directly. And also as a developer or software engineer you should know what you are doing right 😇. Software development is not something you click a button and everything is done for you. So without further do let’s get started.

STEP 1: Let’s open up the eclipse IDE and create a dynamic web project.

STEP 2: Let’s do the basic configurations and create a simple REST API.(I hope you all familiar with how to create a REST API using Java (JAX-RS))

Add following file to the web.xml file inside the WebContent\WEB-INF folder

<servlet><servlet-name>Jersey Web Application</servlet-name><servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class><init-param><param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name><param-value>true</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>Jersey Web Application</servlet-name><url-pattern>/api/v2/*</url-pattern></servlet-mapping>

After adding these lines the web.xml file will look like this.

Let’s convert this project to a maven project. So we can manage our dependencies easily.

Then add the following dependencies to the pom.xml

<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-bundle</artifactId>
<version>1.19.4</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-core</artifactId>
<version>1.19.4</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.19.4</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.8.1</version>
</dependency>
<dependency>
<groupId>com.owlike</groupId>
<artifactId>genson</artifactId>
<version>1.6</version>
</dependency>

STEP 3: Let’s simple REST controller

Ok. Now we have successfully created our first end point. Check whether your end point working or not. Then we can move into our core part which is deploying this into tomcat server using command line.

It’s working 😁.

Let’s export this project into WAR file. So we can deploy it into a tomcat server. (Export it into Desktop or any directory you can easily find)

Now stop tomcat server on eclipse. Then close all eclipse windows.

When you are running this REST API on eclipse, you need some of server to run. Most of the time people choose tomcat. If that is the case you already have tomcat downloaded in your computer. Find it. If you can’t find it download it from here.

https://tomcat.apache.org/download-90.cgi

After downloading tomcat server, copy your .WAR file into webapps folder inside tomcat directory.

We are almost here 🤭.

In default tomcat server run on port 8080. If you want change it you can do it by editing the server.xml file inside the conf folder of tomcat directory.

One last step before starting the tomcat server you have to set JAVA_HOME path variable. Let’s do that now. (If you already done it skip the following steps).

STEP 1 : Type path variable in search menu.

STEP 2: Click on Edit the system environment variables

Now set JAVA_HOME to you JDK directory(not the bin folder. Just the JDK folder)

Now save everything and we are good to go 🥳.

Finally go into bin folder of your tomcat directory. Then open up command prompt. Now type

startup

We have done it. Now check your end point It is working as before. 😎

We have successfully deployed our Java (JAX-RS) REST API into a tomcat server.

--

--

Shihara Dilshan

Associate Technical Lead At Surge Global | React | React Native | Flutter | NodeJS | AWS | Type Script | Java Script | Dart | Go