105

I Started a new project with Spring Boot 1.2.3. I'm getting error

java.lang.NoClassDefFoundError: javax/servlet/Filter

Gradle Dependencies:

dependencies {
    compile("org.springframework.boot:spring-boot-starter-actuator")
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("org.springframework.boot:spring-boot-starter-security")
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.codehaus.groovy:groovy")
    compile("com.h2database:h2")
    compile("org.thymeleaf.extras:thymeleaf-extras-springsecurity3")

    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
    testCompile("org.springframework.boot:spring-boot-starter-test")

    compile 'org.webjars:jquery:2.1.4'
    compile 'org.webjars:bootstrap:3.3.4'
}

Here is the full stack trace

Exception in thread "main" java.lang.NoClassDefFoundError: javax/servlet/Filter
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2570)
    at java.lang.Class.getMethod0(Class.java:2813)
    at java.lang.Class.getMethod(Class.java:1663)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:125)
Caused by: java.lang.ClassNotFoundException: javax.servlet.Filter
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 29 more

Process finished with exit code 1
1
  • 4
    Did you have spring-boot-starter-web in your dependencies? Commented Nov 27, 2019 at 11:25

12 Answers 12

187

for the maven users, comment the scope provided in the following dependency:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <!--<scope>provided</scope>-->
    </dependency>

UPDATE

As feed.me mentioned you have to uncomment the provided part depending on what kind of app you are deploying.

Here is a useful link with the details: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#build-tool-plugins-maven-packaging

Sign up to request clarification or add additional context in comments.

8 Comments

It's important to make this dependency "provided" again before building a war file! Otherwise the war file will crash when it gets deployed (happend to me with tomcat)
I also found I had to reimport the maven project in intellij, presumably to force the dependency to be downloaded with out this additional scope which was causing the application to prang out.
This is all well and good, the problem is that when you then build the war these jars are included and this is not an option. Why can't IntelliJ figure out the proper classpath for running a Gradle + Spring Boot application?
I am faced with the same error in my spring security - I can see the javax.servlet-api in my libraries folder in IDEA but getting the above compiler error. Project downloaded from spring starter
Hey @Aerox, as far as I remember, when you were building a war, you need the provided scope, but if you're building a jar you don't need the scope field. Give it a try, cause this was a while back and in the past couple of years I haven't really used spring boot.
|
57

It's not good to change the scope of your application dependencies. Putting the dependency as compile, will provide the dependency also in your artifact that will be installed somewere. The best think to do is configure the RUN configuration of your sping boot application by specifying as stated in documentation :

"Include dependencies with 'Provided' scope" "Enable this option to add dependencies with the Provided scope to the runtime classpath."

enter image description here

5 Comments

Thanks, It worked once i enabled "Add dependencies with "provided" scope to classpath"
This an old thread, but nevertheless, it still helped us out of the problem. Thanks!
I don't have that checkbox, maybe because I'm on community edition?
can be because of the new version of Intellij.
With Eclipse it was working without any special edit, but for IJ THIS IS THE SOLUTION, thanks a lot
37
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")

This should be

compile("org.springframework.boot:spring-boot-starter-tomcat")

1 Comment

Assuming that the error was at runtime, the providedRuntime's limit was proper. But the use of providedRuntime implies actually providing the ..-tomcat code at runtime. Perhaps, using runtime instead of providedRuntime could do that, without exposing the API that is not used at compile time.
29

It's interesting things with IDE (IntelliJ in this case):

  • if you leave default, i.e. don't declare spring-boot-starter-tomcat as provided, a spring-boot-maven-plugin (SBMP) put tomcat's jars to your war -> and you'll probably get errors deploying this war to container (there could be a versions conflict)

  • else you'll get classpath with no compile dependency on tomcat-embed (SBMP will build executable war/jar with provided deps included anyway)

    • intelliJ honestly doesn't see provided deps at runtime (they are not in classpath) when you run its Spring Boot run configuration (since IJ 2020.3 "include deps with provided scope" option was added in run configuration).
    • and with no tomcat-embed you can't run Spring-Boot with embedded servlet container.

There is some tricky workaround: put Tomcat's jars to classpath of your idea-module via UI: File->Project Structure->(Libraries or Modules/Dependencies tab) .

  • tomcat-embed-core
  • tomcat-embed-el
  • tomcat-embed-websocket
  • tomcat-embed-logging-juli

Better solution for maven case

Instead of adding module dependencies in Idea, it is better to declare maven profile with compile scope of spring-boot-starter-tomcat library.

<profiles>
    <profile>
        <id>embed-tomcat</id>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
                <scope>compile</scope>
            </dependency>
        </dependencies>
    </profile>
</profiles>

while spring-boot-starter-tomcat was declared provided in <dependencies/>, making this profile active in IDE or CLI (mvn -Pembed-tomcat ...) allow you to launch build with embedded tomcat.

4 Comments

your update solves the problem, but when you create a war file (production arctifact) you have to keep in mind that you have to activate the profile. which is not automatically done for you.
If your production artifact (PA) needs to be with embedded tomcat (ET) inside (in .war), it is not necessary to declare spring-boot-starter-tomcat as provided dependency. My answer explain the case when we need PA with no ET (as it had to be deployed on some container itself) and want to locally start SpringBootApplication with embed-tomcat in IntelliJ with its run-configurations.
I think the update is the correct solution for this problem in my opinion: you can build the war without embedded Tomcat and can use the profile inside IntelliJ. Very elegant and no commenting/uncommenting needed
in new version I see a checkbox, checking it will added provided dependencies at runtime for intellij !
17

In my case scope of that dependency was provided.

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

So, I was getting the same issue. Caused by: java.lang.ClassNotFoundException: javax.servlet.Filter

I had to enable the below option in Intellij

Add dependencies with "provided" scope to classpath

enter image description here

You will see below option enabled,

enter image description here

OR

You can add the below property to .intellij/workspace.xml file under your application configuration section. <option name="INCLUDE_PROVIDED_SCOPE" value="true" />

e.g.,

  <component name="RunManager" selected="Application.DemoApp">
    <configuration name="DemoApp" type="Application" factoryName="Application">
      <option name="INCLUDE_PROVIDED_SCOPE" value="true" /> ```

2 Comments

Thanks mate for the solution. Have been banging my head for past 2 days on this.
Amazing, that was the actual issue in my case too. The project worked on STS but not on IntelliJ. Thanks again.
15

Add the following dependency. The scope should be compile then it will work.

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

2 Comments

ThQ so much it helps me :)
I just changed the scope from provided to compile and it works fine. Thanks much.
7

For Jar

Add pom.xml

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

1 Comment

Thanks - any idea why this was not autoadded?
3

2023 Solution

Remove provided from maven of tomcat artifactId

 <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <!--   Here <scope>provided</scope  >-->
    </dependency>

Comments

3

With IntelliJ 2024.1.1, you can configure Run to do that: enter image description here

Comments

2

That looks like you tried to add the libraries servlet.jar or servlet-api.jar into your project /lib/ folder, but Tomcat already should provide you with those libraries. Remove them from your project and classpath. Search for that anywhere in your project or classpath and remove it.

Comments

1

The configuration here is working for me:

configurations {
    customProvidedRuntime
}

dependencies {
    compile(
        // Spring Boot dependencies
    )

    customProvidedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
}

war {
    classpath = files(configurations.runtime.minus(configurations.customProvidedRuntime))
}

springBoot {
    providedConfiguration = "customProvidedRuntime"
}

1 Comment

this worked for me but i had to remove the springBoot config. part
1

Just comment the scope in spring boot dependency as shown below

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <!--<scope>provided</scope>-->
</dependency>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.