zengkid / SmartTomcat

The Tomcat plugin for Intellij IDEA
Apache License 2.0
126 stars 55 forks source link

NoClassDefFoundError: javax/servlet/ServletContainerInitializer #102

Closed OnelSarmiento closed 1 year ago

OnelSarmiento commented 1 year ago

After updating the Intellij and Plugin I start encountering this error. but the compiled war file is working properly when deployed directly to tomcat.

Console Log Error: image

for some reason, it's looking for javax.servlet.ServletContainerInitializer but I'm using Jakarta servlet.

SmartTomcat Configuration: image

I also tried to update and reinstall everything including JDK and Tomcat and still the same.

gfc.log

OnelSarmiento commented 1 year ago

Upon further investigation, it looks like the plugin is not able to generate the required files inside ".SmartTomcat" directory. I'm able to resolve my issue by overriding the default parameters of the plugin. (CATALINA_HOME, CATALINA_BASE)

image

yuezk commented 1 year ago

Under the hood, the plugin is trying to construct the parameters you specified manually. I have no idea why it fails to do that.

OnelSarmiento commented 1 year ago

Hi @yuezk,

I'm still encountering this issue after the update. my workaround above works but I had to deploy the war file in the Tomcat directory every time I have code changes.

I included my Smart Tomcat Configuration and Log below. My project is also based on Spring MVC with Correto 17.

Smart Tomcat Configuration:

<component name="ProjectRunConfigurationManager">
  <configuration default="false" name="Tomcat2" type="com.poratu.idea.plugins.tomcat">
    <log_file alias="Tomcat Catalina Log" path="$USER_HOME$/.SmartTomcat/gfc_main/GFC/logs/catalina.*" />
    <predefined_log_file enabled="true" id="Tomcat Localhost Log" />
    <predefined_log_file enabled="true" id="Tomcat Access Log" />
    <predefined_log_file id="Tomcat Manager Log" />
    <predefined_log_file id="Tomcat Host Manager Log" />
    <option name="contextPath" value="/GFC" />
    <option name="docBase" value="C:\Users\lsarmiento\Documents\gfc_java\gfc_main\src\main\webapp" />
    <option name="envOptions">
      <map />
    </option>
    <option name="extraClassPath" value="" />
    <option name="tomcatInfo">
      <TomcatInfo>
        <option name="name" value="Apache Tomcat/10.1.7" />
        <option name="path" value="$PROJECT_DIR$/../../../../../Program Files/Apache Software Foundation/Tomcat 10.1" />
        <option name="version" value="10.1.7.0" />
      </TomcatInfo>
    </option>
    <option name="vmOptions" value="&quot;-Dlog4j.configurationFile=${GFC_HOME}\log4j2.xml&quot; &quot;-Xms12800m&quot; &quot;-Xmx25600m&quot;" />
    <module name="GFC" />
    <method v="2">
      <option name="Make" enabled="true" />
    </method>
  </configuration>
</component>

Log:

"C:\Program Files\Amazon Corretto\jdk17.0.5_8\bin\java.exe" -Dlog4j.configurationFile=C:\GFC\resources\props\log4j2.xml -Xms12800m -Xmx25600m "-Dcatalina.home=C:\Program Files\Apache Software Foundation\Tomcat 10.1" -Dcatalina.base=C:\Users\lsarmiento\.SmartTomcat\gfc_main\GFC -Djava.io.tmpdir=C:\Users\lsarmiento\.SmartTomcat\gfc_main\GFC\temp -Djava.util.logging.config.file=C:\Users\lsarmiento\.SmartTomcat\gfc_main\GFC\conf\logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.3.2\lib\idea_rt.jar=60302:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.3.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Apache Software Foundation\Tomcat 10.1\bin\bootstrap.jar;C:\Program Files\Apache Software Foundation\Tomcat 10.1\bin\tomcat-juli.jar" org.apache.catalina.startup.Bootstrap start
NOTE: Picked up JDK_JAVA_OPTIONS: --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
13-Apr-2023 18:40:46.365 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name:   Apache Tomcat/10.1.7
13-Apr-2023 18:40:46.371 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Feb 27 2023 20:25:27 UTC
13-Apr-2023 18:40:46.371 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version number: 10.1.7.0
13-Apr-2023 18:40:46.372 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Windows 11
13-Apr-2023 18:40:46.372 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            10.0
13-Apr-2023 18:40:46.372 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
13-Apr-2023 18:40:46.372 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             C:\Program Files\Amazon Corretto\jdk17.0.5_8
13-Apr-2023 18:40:46.373 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           17.0.5+8-LTS
13-Apr-2023 18:40:46.373 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Amazon.com Inc.
13-Apr-2023 18:40:46.373 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         C:\Users\lsarmiento\.SmartTomcat\gfc_main\GFC
13-Apr-2023 18:40:46.373 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         C:\Program Files\Apache Software Foundation\Tomcat 10.1
13-Apr-2023 18:40:46.389 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.lang=ALL-UNNAMED
13-Apr-2023 18:40:46.389 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.io=ALL-UNNAMED
13-Apr-2023 18:40:46.390 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.util=ALL-UNNAMED
13-Apr-2023 18:40:46.390 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.util.concurrent=ALL-UNNAMED
13-Apr-2023 18:40:46.390 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
13-Apr-2023 18:40:46.390 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dlog4j.configurationFile=C:\GFC\resources\props\log4j2.xml
13-Apr-2023 18:40:46.390 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Xms12800m
13-Apr-2023 18:40:46.390 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Xmx25600m
13-Apr-2023 18:40:46.390 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=C:\Program Files\Apache Software Foundation\Tomcat 10.1
13-Apr-2023 18:40:46.390 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=C:\Users\lsarmiento\.SmartTomcat\gfc_main\GFC
13-Apr-2023 18:40:46.391 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=C:\Users\lsarmiento\.SmartTomcat\gfc_main\GFC\temp
13-Apr-2023 18:40:46.391 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=C:\Users\lsarmiento\.SmartTomcat\gfc_main\GFC\conf\logging.properties
13-Apr-2023 18:40:46.391 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
13-Apr-2023 18:40:46.391 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.3.2\lib\idea_rt.jar=60302:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.3.2\bin
13-Apr-2023 18:40:46.391 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dfile.encoding=UTF-8
13-Apr-2023 18:40:46.400 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent The Apache Tomcat Native library which allows using OpenSSL was not found on the java.library.path: [C:\Program Files\Amazon Corretto\jdk17.0.5_8\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Windows\System32;C:\Windows;C:\Windows\System32\wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\Azure Data Studio\bin;C:\Program Files\Microsoft SQL Server\150\Tools\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\150\Tools\Binn\;C:\Program Files\Microsoft SQL Server\150\DTS\Binn\;C:\Strawberry\c\bin;C:\Strawberry\perl\site\bin;C:\Strawberry\perl\bin;C:\Program Files (x86)\GnuPG\bin;C:\Program Files\Git\cmd;C:\Program Files\Amazon\AWSCLIV2\;C:\Program Files\Amazon\SessionManagerPlugin\bin\;C:\apache-maven-3.8.6\bin;C:\Program Files (x86)\Microsoft SQL Server\160\DTS\Binn\;C:\Program Files\Amazon Corretto\jdk17.0.5_8\bin;C:\Users\lsarmiento\AppData\Local\Microsoft\WindowsApps;;C:\Users\lsarmiento\AppData\Local\Programs\Microsoft VS Code\bin;C:\Program Files\Azure Data Studio\bin;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2022.3.2\bin;;.]
13-Apr-2023 18:40:46.833 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
13-Apr-2023 18:40:46.863 INFO [main] org.apache.catalina.startup.Catalina.load Server initialization in [772] milliseconds
13-Apr-2023 18:40:46.950 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
13-Apr-2023 18:40:46.951 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/10.1.7]
13-Apr-2023 18:40:46.979 INFO [main] org.apache.catalina.startup.HostConfig.deployDescriptor Deploying deployment descriptor [C:\Users\lsarmiento\.SmartTomcat\gfc_main\GFC\conf\Catalina\localhost\GFC.xml]
13-Apr-2023 18:40:48.456 SEVERE [main] org.apache.catalina.startup.HostConfig.deployDescriptor Error deploying deployment descriptor [C:\Users\lsarmiento\.SmartTomcat\gfc_main\GFC\conf\Catalina\localhost\GFC.xml]
    java.lang.IllegalStateException: Error starting child
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:686)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:658)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:713)
        at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:641)
        at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1927)
        at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
        at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
        at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:123)
        at org.apache.catalina.startup.HostConfig.deployDescriptors(HostConfig.java:534)
        at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:424)
        at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1656)
        at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:309)
        at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123)
        at org.apache.catalina.util.LifecycleBase.setStateInternal(LifecycleBase.java:423)
        at org.apache.catalina.util.LifecycleBase.setState(LifecycleBase.java:366)
        at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:898)
        at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:846)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1332)
        at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1322)
        at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
        at org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75)
        at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:145)
        at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:871)
        at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:241)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.core.StandardService.startInternal(StandardService.java:428)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:913)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        at org.apache.catalina.startup.Catalina.start(Catalina.java:795)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:568)
        at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:347)
        at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:478)
    Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/GFC]]
        at org.apache.catalina.util.LifecycleBase.handleSubClassException(LifecycleBase.java:440)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:198)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:683)
        ... 37 more
    Caused by: java.lang.NoClassDefFoundError: javax/servlet/ServletContainerInitializer
        at java.base/java.lang.ClassLoader.defineClass1(Native Method)
        at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1012)
        at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150)
        at org.apache.catalina.loader.WebappClassLoaderBase.findClassInternal(WebappClassLoaderBase.java:2487)
        at org.apache.catalina.loader.WebappClassLoaderBase.findClass(WebappClassLoaderBase.java:865)
        at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1401)
        at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1245)
        at java.base/java.lang.Class.forName0(Native Method)
        at java.base/java.lang.Class.forName(Class.java:467)
        at org.apache.catalina.startup.WebappServiceLoader.loadServices(WebappServiceLoader.java:235)
        at org.apache.catalina.startup.WebappServiceLoader.load(WebappServiceLoader.java:206)
        at org.apache.catalina.startup.ContextConfig.processServletContainerInitializers(ContextConfig.java:1834)
        at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1299)
        at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:987)
        at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:304)
        at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:123)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:4797)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        ... 38 more
    Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletContainerInitializer
        at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1437)
        at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1245)
        ... 56 more
13-Apr-2023 18:40:48.460 INFO [main] org.apache.catalina.startup.HostConfig.deployDescriptor Deployment of deployment descriptor [C:\Users\lsarmiento\.SmartTomcat\gfc_main\GFC\conf\Catalina\localhost\GFC.xml] has finished in [1,481] ms
13-Apr-2023 18:40:48.465 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
13-Apr-2023 18:40:48.684 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [1820] milliseconds
http://localhost:8080/GFC
yuezk commented 1 year ago

I noticed that you're using Tomcat 10, which has changed the package javax.servlet to jakarta.servlet. Did you use Springframework 6 since Springframework 5 doesn't support Tomcat 10.

OnelSarmiento commented 1 year ago

I'm using Spring Framework 6, but after going through my dependencies 1 by 1 I found out that log4j-web is what causing the issue.

after changing it from log4j-web to log4j-Jakarta-web it's finally working.

From:

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-web</artifactId>
            <version>${log4j.version}</version>
        </dependency>

Changes To:

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-jakarta-web</artifactId>
            <version>${log4j.version}</version>
        </dependency>