Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code works with Embedded Apache Tomcat 8 but not with 9. What's changed?

Embedding Apache Tomcat into an eclipse web app project.
The code works when I'm using the latest Tomcat 8 (8.0.5 Embedded) jars as dependencies, and this server responds at http://localhost:8080, however, it fails to start the same way and does not respond in this address when using the latest Tomcat 9's (9.0.5 Embedded) jars.
The code is very simple. I've researched as thoroughly as I could but didn't figure out what's wrong.

package app;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;

public class Main {

    public static void main(String[] args) {

        Tomcat tomcat = new Tomcat();
        tomcat.setPort(8080);

        try {
            tomcat.start();
        } catch (LifecycleException e) {
            e.printStackTrace();
        }

        tomcat.getServer().await();
    }

}

console output when using Tomcat 9.0.5 Embedded jars:

org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Tomcat]

console output when using Tomcat 8.0.5 Embedded jars:

org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
org.apache.catalina.core.StandardService startInternal
INFO: Starting service Tomcat
org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
like image 621
Benjq Avatar asked Feb 26 '18 22:02

Benjq


People also ask

What is the difference between Tomcat 8 and Tomcat 9?

There isn't a lot of difference between Tomcat 8.5 and 9.0: the former started as a fork of the first pre-release versions of Tomcat 9.0. Since the big changes in Java EE Servlet API came with version 3.0 (which is supported by Tomcat 7.0) I don't foresee any major problems with the migration to a newer Tomcat release.

Is Tomcat 9 backwards compatible?

x noteable changes. The Tomcat developers aim for each patch release to be fully backwards compatible with the previous release.

What is the difference between Tomcat 9 and Tomcat 10?

Apache Tomcat 9 and 10 are equivalent products. The only difference is support for changes for the package names in the Jakarta Servlet and related technologies from javax. * to jakarta.

What is the advantage of having an embedded Tomcat server?

Embedded Tomcat offers a way to package Java web applications that is consistent with a microservices-based approach to software development. It also makes it easier to distribute Java web applications through Docker containers and manage them through a container orchestration service, such as Kubernetes or OpenShift.


1 Answers

It looks like you haven't added a Connector to your embedded server. Tomcat 9 no longer automatically adds a Connector to your server for you, so you'll have to trigger it yourself:

package app;

import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;

public class Main {

    public static void main(String[] args) {

        Tomcat tomcat = new Tomcat();
        tomcat.setPort(8080);
        tomcat.getConnector(); // Trigger the creation of the default connector

        try {
            tomcat.start();
        } catch (LifecycleException e) {
            e.printStackTrace();
        }

        tomcat.getServer().await();
    }
}

It's worth mentioning that adding a call to tomcat.getConnector() should be safe for previous versions of Tomcat as well, so this need not be a "Tomcat 9-only" thing.

like image 105
Christopher Schultz Avatar answered Oct 19 '22 02:10

Christopher Schultz