Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedded tomcat java application is running, but server cannot be reached

I am trying to set up a basic embedded Tomcat server and am unable to get the Tomcat server to run.

public class Main {

    public static void main(String[] args) throws LifecycleException {

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

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

Running this java app in Eclipse provides the output:

June 19, 2019 12:00:00 PM org.apache.catalina.core.StandardService startInternal

INFO: Starting service [Tomcat]

And then waits until I hit stop, as expected, but when I run curl localhost:8888 in the terminal, i get curl: (7) Failed connect to localhost:8888; Connection refused.

I've followed this tutorial exactly, but I cannot seem to get the server to actually run. Also, netstat -nlt does not show the port 8888 being open.

My build.gradle has a single dependency:

implementation 'org.apache.tomcat.embed:tomcat-embed-core:9.0.21'

Is there something I'm missing here?

like image 693
Matt Avatar asked Jun 19 '19 13:06

Matt


2 Answers

I just found solution I use tomcat 9, and it looks like we need to call method getConnector() on tomcat object.

    Tomcat tomcat = new Tomcat();
    tomcat.setPort(8080);
    tomcat.getConnector();
like image 147
Alexander Kondaurov Avatar answered Oct 09 '22 13:10

Alexander Kondaurov


Tomcat 9 no longer adds a Connector by default like previous versions. You can do the following to add one:

final Connector connector = new Connector();
connector.setPort(port);
tomcat.getService().addConnector(connector);

As mentioned in another answer, you can also call tomcat.getConnector(); which is a side-affecting getter.

like image 6
Travis Schneeberger Avatar answered Oct 09 '22 15:10

Travis Schneeberger