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"]
4
  • If you tested them one after the other, did you kill the first one that already listens on port 8080?
    – SHG
    Feb 26, 2018 at 23:07
  • 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.
    – Benjq
    Feb 26, 2018 at 23:17
  • 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? Feb 27, 2018 at 14:42
  • 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.
    – Benjq
    Feb 28, 2018 at 10:10

1 Answer 1

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.

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.
    – Benjq
    Feb 28, 2018 at 10:27
  • @Benjq The best place to ask is the Tomcat users' mailing list. Feb 28, 2018 at 15:20
  • 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?
    – ghoulfolk
    Jan 10, 2020 at 12:10

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.