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

1

14

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"]
Acea answered 26/2, 2018 at 22:51 Comment(4)
If you tested them one after the other, did you kill the first one that already listens on port 8080?Degraded
Yes. The procedure I followed was to first stop the running program, then change dependencies and then re-run. The results were the same through many trials with the 8 working but 9 not.Acea
Something is horribly wrong with your version numbers. Latest Tomcat 8.0.x is 8.0.50 and latest Tomcat 8.5.x is 8.5.28. What version are you actually running?Darladarlan
Indeed I meant version 8.0.50 when referring to Embedded Tomcat 8. I did try 8.5.28 as well and it also works. The problems of course start with 9.Acea
D
40

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.

Darladarlan answered 27/2, 2018 at 14:47 Comment(3)
Kudos! This seems to be the correct answer as far as what has changed in Tomcat 9. Adding the tomcat.getConnector() made the server start & respond. It does however error with 500 and I need to configure this with jersey for a RESTful system. I would highly appreciate it if you or anyone could direct me to where I could further educate myself on the correct way to work with the connector for this purpose. I searched myself but the results were either useless, outdated or irrelevant for my purpose.Acea
@Acea The best place to ask is the Tomcat users' mailing list.Darladarlan
This worked like a charm on my local setting. As a related question though, How can I add this to a linux server where the tomcat was also raised from 8 to 9? should it go somewhere in server.xml?Unknown

© 2022 - 2024 — McMap. All rights reserved.