Home > Enterprise >  Getting java.lang.ClassNotFoundException: jakarta.servlet.ServletContext
Getting java.lang.ClassNotFoundException: jakarta.servlet.ServletContext

Time:02-05

I installed Intellij on my new laptop and I attempted to run a very simple Spring Boot web app from a course i cloned. When I run the app, I keep getting the same java.lang.IllegalStateException: Failed to introspect Class [org.springframework.web.context.support.GenericWebApplicationContext] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@2f0e140b] followed by java.lang.ClassNotFoundException: jakarta.servlet.ServletContext. Everything works fine when i run the app in (for example) Visual Studio Code.

My pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.ltp</groupId>
    <artifactId>global-superstore</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>global-superstore</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>6.0.4</version>
        </dependency>

        <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <version>5.0.0</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>repo1.maven.org</id>
            <url>https://repo1.maven.org/maven2/</url>
        </repository>
    </repositories>
    
</project>

I tried downloading multiple versions of jakarta.servlet-api and I get the same error every time. When I download jakarta.servlet-api, I don't get ServletContext interface in jakarta/servlet folder. Is there something I'm missing?

Here is the full stack trace:

java.lang.IllegalStateException: Failed to introspect Class [org.springframework.web.context.support.GenericWebApplicationContext] from ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader@2f0e140b]
    at org.springframework.util.ReflectionUtils.getDeclaredFields(ReflectionUtils.java:743) ~[spring-core-5.3.19.jar:5.3.19]
    at org.springframework.util.ReflectionUtils.findField(ReflectionUtils.java:611) ~[spring-core-5.3.19.jar:5.3.19]
    at org.springframework.boot.devtools.restart.ClassLoaderFilesResourcePatternResolver.retrieveResourceLoader(ClassLoaderFilesResourcePatternResolver.java:81) ~[spring-boot-devtools-2.6.7.jar:2.6.7]
    at org.springframework.boot.devtools.restart.ClassLoaderFilesResourcePatternResolver.<init>(ClassLoaderFilesResourcePatternResolver.java:77) ~[spring-boot-devtools-2.6.7.jar:2.6.7]
    at org.springframework.boot.devtools.restart.Restarter.prepare(Restarter.java:442) ~[spring-boot-devtools-2.6.7.jar:2.6.7]
    at org.springframework.boot.devtools.restart.Restarter.prepare(Restarter.java:430) ~[spring-boot-devtools-2.6.7.jar:2.6.7]
    at org.springframework.boot.devtools.restart.RestartApplicationListener.onApplicationPreparedEvent(RestartApplicationListener.java:100) ~[spring-boot-devtools-2.6.7.jar:2.6.7]
    at org.springframework.boot.devtools.restart.RestartApplicationListener.onApplicationEvent(RestartApplicationListener.java:53) ~[spring-boot-devtools-2.6.7.jar:2.6.7]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176) ~[spring-context-5.3.19.jar:5.3.19]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169) ~[spring-context-5.3.19.jar:5.3.19]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143) ~[spring-context-5.3.19.jar:5.3.19]
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:131) ~[spring-context-5.3.19.jar:5.3.19]
    at org.springframework.boot.context.event.EventPublishingRunListener.contextLoaded(EventPublishingRunListener.java:103) ~[spring-boot-2.6.7.jar:2.6.7]
    at org.springframework.boot.SpringApplicationRunListeners.lambda$contextLoaded$4(SpringApplicationRunListeners.java:74) ~[spring-boot-2.6.7.jar:2.6.7]
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1511) ~[na:na]
    at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:120) ~[spring-boot-2.6.7.jar:2.6.7]
    at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:114) ~[spring-boot-2.6.7.jar:2.6.7]
    at org.springframework.boot.SpringApplicationRunListeners.contextLoaded(SpringApplicationRunListeners.java:74) ~[spring-boot-2.6.7.jar:2.6.7]
    at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:408) ~[spring-boot-2.6.7.jar:2.6.7]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:302) ~[spring-boot-2.6.7.jar:2.6.7]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1312) ~[spring-boot-2.6.7.jar:2.6.7]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301) ~[spring-boot-2.6.7.jar:2.6.7]
    at com.ltp.globalsuperstore.GlobalSuperstoreApplication.main(GlobalSuperstoreApplication.java:10) ~[classes/:na]
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:578) ~[na:na]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) ~[spring-boot-devtools-2.6.7.jar:2.6.7]
Caused by: java.lang.NoClassDefFoundError: jakarta/servlet/ServletContext
    at java.base/java.lang.Class.getDeclaredFields0(Native Method) ~[na:na]
    at java.base/java.lang.Class.privateGetDeclaredFields(Class.java:3353) ~[na:na]
    at java.base/java.lang.Class.getDeclaredFields(Class.java:2424) ~[na:na]
    at org.springframework.util.ReflectionUtils.getDeclaredFields(ReflectionUtils.java:738) ~[spring-core-5.3.19.jar:5.3.19]
    ... 25 common frames omitted
Caused by: java.lang.ClassNotFoundException: jakarta.servlet.ServletContext
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) ~[na:na]
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) ~[na:na]
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521) ~[na:na]
    ... 29 common frames omitted

CodePudding user response:

Your pom looks good. Try the suggestion from this post: Force Intellij IDEA to reread all maven dependencies. From what I can tell your intelij is has not loaded the dependency into your env yet.

  • Related