Published on Feb 9 2018 in Java Tomcat

We build proof-of-concept JAR and WAR Spring Boot application and show how to run it on shared cPanel server using any of our Java hosting packages.

We use JDK 8 and Maven 3.2 that are available with any Java hosting package. No IDE will be used but commands typed directly in shell for this simple task.

Check that Java and Maven are ready:

java -version
mvn -v

The below POM allows for generating JAR and WAR with only 1 change: the packaging attribute. First we POM will generate JAR. Later we will update it so that it generates a WAR i.e. add <packaging>war</packaging>. For testing WAR we will use a standalone Tomcat that runs beside.

cat > pom.xml<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""




    <!-- Add Tomcat and Spring MVC -->

        <!-- Skip the following dependency if you are not going to use the application with external Tomcat. Embedded Tomcat does not need it. -->


    <!-- We defintely need below for Maven to process Spring Boot apps. -->


Embedded servlet container dependency is marked as 'provided'. Generated WAR will contain dependencies marked this way in lib-provided directory. In effect you will also be able to run the application using java -jar that we will test below.

Now the Java code. @RestController will allow us to skip creation of view file (template) needed for regular @Controller. @RequestMapping("/") maps application's root URL (not domain URL) to the hello function. If the application will be deployed as ROOT.war then application's root URL will equal domain's root URL.

mkdir -p src/main/java

cat > src/main/java/<<EOF
package com.example;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;

public class Example extends SpringBootServletInitializer {

    String hello() {
        return "Hello World from JAR or WAR!";

    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Example.class);

    public static void main(String[] args) throws Exception {, args);


Testing embedded Tomcat via 'spring-boot:run'

mvn spring-boot:run

Ctrl+C will shutdown the embedded server. On another terminal reach the URL:

Hello World from JAR or WAR!

Testing JAR at custom port

mvn package
java $JAVA_OPTS -Dserver.port=11188 -jar target/SpringBootTester-0.1.jar 
Hello World from JAR or WAR!

Ctrl+C will shutdown the process if it runs in foreground. Of course you can leave it running in background and logging to a file by appending &>log & at the end of the java -jar ... command.

Testing WAR in external Tomcat listening on default HTTP port

We need to modify packaging line first.

sed -i -re 's/(<packaging>)jar(<\/packaging>)/\1war\2/' pom.xml
mvn clean package
cp target/SpringBootTester-0.1.war $CATALINA_HOME/webapps

Wait until it deploys by tracing log with tail -f $CATALINA_HOME/logs/catalina.out. On another terminal check:

Hello World from JAR or WAR!

You might also have copied it to $CATALINA_HOME/webapps/ROOT.war to have it served at domain's root URL. See how main class changes in MANIFEST.MF depending on what you set in pom.xml:

unzip -c target/SpringBootTester-0.1.war META-INF/MANIFEST.MF | grep Class
Start-Class: com.example.Example
Main-Class: org.springframework.boot.loader.WarLauncher

unzip -c target/SpringBootTester-0.1.jar META-INF/MANIFEST.MF | grep Class
Start-Class: com.example.Example
Main-Class: org.springframework.boot.loader.JarLauncher

There are many alternative ways of presetting server.port for example in src/main/resources/


or in src/main/resources/application.yml

  port: 8888

Here are the SpringBootTester-0.1.jar and SpringBootTester-0.1.war for your reference.