2

Following the tutorial given here: https://www.codejava.net/servers/tomcat/how-to-embed-tomcat-server-into-java-web-applications, I am attempting to deploy a Spring war file in a separate embedded tomcat configured project. Here's my java configuration and my build.gradle configuration:

Java Config for Embedded Tomcat:

public static void main(String[] args) throws LifecycleException {
        Tomcat tomcat = new Tomcat();
        tomcat.setBaseDir("temp");
        tomcat.setPort(9000);

        String contextPath = "";
        String docBase = new File("spring.war").getAbsolutePath();

        tomcat.getHost().setAppBase(".");

        tomcat.addWebapp(contextPath, docBase);

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

Gradle build file:

plugins {
    id 'java'
    id 'application'
}

group 'org.example'
version '1.0-SNAPSHOT'

mainClassName = 'TomcatDriver'

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.apache.tomcat.embed', name: 'tomcat-embed-core', version: '9.0.37-dev'
    compile group: 'org.apache.tomcat.embed', name: 'tomcat-embed-jasper', version: '9.0.37'
    compile group: 'org.apache.tomcat', name: 'tomcat-juli', version: '9.0.37'
    compile group: 'org.apache.tomcat', name: 'tomcat-annotations-api', version: '9.0.37'
}

When I run from Intellij Idea (Community), the app does seem to initialize somewhat, but visiting the specified address on the browser gives error:

This site can’t be reached
localhost refused to connect.
Try:
Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_REFUSED

Here's the log that I get when running the app using Intellij Idea (Community):

17:13:32: Executing task 'TomcatDriver.main()'...

> Task :compileJava
> Task :processResources NO-SOURCE
> Task :classes

> Task :TomcatDriver.main()
Jan 20, 2021 5:13:34 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Tomcat]
Jan 20, 2021 5:13:34 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet engine: [Apache Tomcat/9.0.37]
Jan 20, 2021 5:13:34 PM org.apache.catalina.startup.ContextConfig getDefaultWebXmlFragment
INFO: No global web.xml found
Jan 20, 2021 5:13:37 PM org.apache.jasper.servlet.TldScanner scanJars
INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Jan 20, 2021 5:13:37 PM org.apache.catalina.core.ApplicationContext log
INFO: 1 Spring WebApplicationInitializers detected on classpath
Jan 20, 2021 5:13:37 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring FrameworkServlet 'dispatcher'
Jan 20, 2021 5:13:38 PM org.hibernate.jpa.internal.util.LogHelper logPersistenceUnitInformation
INFO: HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]
Jan 20, 2021 5:13:39 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.3.7.Final}
Jan 20, 2021 5:13:39 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Jan 20, 2021 5:13:39 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.4.Final}
Jan 20, 2021 5:13:40 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Jan 20, 2021 5:13:42 PM org.hibernate.validator.internal.util.Version <clinit>
INFO: HV000001: Hibernate Validator 6.1.7.Final

Can anyone suggest what I'm doing wrong in my configuration? The same configuration works for non-Spring projects with embedded tomcat of version 8 https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-core/8.0.48.

Embedded Tomcat 8 jars don't seem to run Spring projects. I read here Spring boot WAR deployment failed to start in Tomcat that Spring war files may require Tomcat 9 for deployment. This is why I am trying with tomcat 9 jars.

UPDATE

I think this post solves the problem that I was having: Code works with Embedded Apache Tomcat 8 but not with 9. What's changed?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Musa Khan
  • 21
  • 4

0 Answers0