首页 > 编程语言 >java.lang.ClassNotFoundException: javax.servlet.Filter

java.lang.ClassNotFoundException: javax.servlet.Filter

时间:2022-12-20 23:06:44浏览次数:73  
标签:lang ClassNotFoundException java URLClassLoader spring ClassLoader boot

java.lang.ClassNotFoundException: javax.servlet.Filter:有两个原因:
(1)在maven中的作用域,不能是provided,需要是compile就是默认的scope
(2)spring-boot-starter-tomcat的版本bug,换个新的或旧的版本试试

 

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

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​

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) 

​http://stackoverflow.com/questions/30374316/spring-boot-java-lang-noclassdeffounderror-javax-servlet-filter​

Springboot project runs fine when running from terminal using command mvn spring-boot:run

Running from Intellij is when I was getting an exception. I changed the spring-boot-starter-tomcat in pom.xml to an older version and it runs without any exception. I think the issue was with compatibly for latest maven embed tomcat 1.2.5.RELEASE.

Now I am using below in my pom.xml

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<version>1.2.4.RELEASE</version>
</dependency>

 



标签:lang,ClassNotFoundException,java,URLClassLoader,spring,ClassLoader,boot
From: https://blog.51cto.com/u_15147537/5956955

相关文章

  • Java中 Long 与 Integer 之间的转换 [ClassCastException:Long cannot be cast to i
     [ClassCastException:Longcannotbecasttointeger]有时数据库操作时经常会遇到这个问题,Long和Integer 都是java中的包装类型,包装类型是不能强制转换的。//Inte......
  • java中 JSONArray 与 List 相互转换
     1.JSONArray转ListList<T>list=JSONObject.parseArray(array.toJSONString(),T.class);//转换语句1 List<T>list=JSONArray.parseArray(array.toJSONString......
  • 【关于Java中方法重写的注意事项】
    需要重写的场景:源代码封装方法无法满足我们的需要,可以通过重写方法解决。注意事项:一般来说,子类只能够重写父类的声明为public和protected的非final方法,如果需要重写......
  • Java面试题
    1Java基础知识面试题(2020最新版)2 Java集合容器面试题(2020最新版)3 Java异常面试题(2020最新版)5 JVM面试题(2020最新版)6 Spring面试题(2020最新版)7 SpringMVC面试题(2020最新版) ......
  • xxl-job定时调度任务Java代码分析
    简介用xxl-job做后台任务管理,主要是快速解决定时任务的HA问题,项目代码量不大,功能精简,没有特殊依赖.因为产品中用到了这个项目,上午花了点时间研究了一下运行机......
  • java中的接口
    本文主要讲述java的接口,以其相关细节。老韩接口的介绍:老韩接口的细节:老韩继承类vs接口实现老韩接口的多态性 1)接口的多态性示意图,如下图所......
  • java命令--jmap命令使用
    jdk安装后会自带一些小工具,jmap命令(JavaMemoryMap)是其中之一。主要用于打印指定Java进程(或核心文件、远程调试服务器)的共享对象内存映射或堆内存细节。jmap命令可以获......
  • java8-时间相关代码整理
    获取昨天起止时刻,就是0点和23点59分59秒LocalDateTime.now().minus(Period.ofDays(1)).with(LocalTime.MIN)LocalDateTime.now().minus(Period.ofDays(1)).with(LocalTi......
  • Java学习笔记1
    1.注释​ 注释是对代码的解释和说明文字。Java中的注释分为三种:单行注释://这是单行注释文字多行注释:/*这是多行注释文字这是多行注释文字这是多行注释文字......
  • JAVA循环结构
    什么是循环:重复的去执行某一件事情while(条件){//循环操作}例如:打印50份卷子1、确定循环内容以及循环条件循环内容:打印卷子循环......