2015-05-21 51 views
29

我使用Spring Boot 1.2.3开始了一个新项目。我收到提示Spring Boot java.lang.NoClassDefFoundError:javax/servlet/Filter

java.lang.NoClassDefFoundError: javax/servlet/Filter 

摇篮依赖关系:

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' 
} 

以下是完整的堆栈跟踪

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 

回答

2

这看起来像你试图库servlet.jarservlet-api.jar添加到您的项目/lib/文件夹,但Tomcat已经应该为您提供这些库。从您的项目和类路径中删除它们。在您的项目或类路径中的任意位置搜索并删除它。

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

这应该是

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

我尝试这样做了,但仍表现出同样的错误 – Ibrahim

91

的Maven用户, 评论在以下依赖规定的范围:

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

UPDATE

由于feed.me提到你不得不取消注释他提供的部分取决于您正在部署的应用程序类型。

下面是详细信息的有用链接:(在这种情况下的IntelliJ) http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#build-tool-plugins-maven-packaging

+9

使这种依赖建立一个WAR文件之前再次“规定”是很重要的!否则,战争文件将在部署时崩溃(使用tomcat发生在我身上) –

+1

我还发现我必须重新导入intellij中的maven项目,这大概是为了强制依赖被下载,而这个额外的范围导致应用程序prang出来。 – Opentuned

+1

这一切都很好,问题在于,当你构建战争时,这些罐子都包含在内,这不是一种选择。为什么IntelliJ不能找出正确的类路径来运行Gradle + Spring Boot应用程序? –

10

很有趣的事情与IDE:

  • 如果你离开默认

    ,即不声明spring-boot-starter-tomcat as provided,a spring-boot-maven-plugin(SBMP)把tomcat的jar放到你的war - >和你可能会得到错误,部署此战容器(有可能是一个版本冲突)

  • 不然你会类路径没有编译依赖Tomcat的嵌入(SBMP将建立可执行的战争/罐与反正包括提供DEPS

    • 的IntelliJ hornestly没有看到提供DEPS在运行时(他们是不是在classpath中),当您运行其SpringBoot 运行配置
    • 并没有Tomcat的嵌入你不能用嵌入式servlet容器运行弹簧引导。

有一些解决方法:把Tomcat的jar文件到你的想法模块的类路径通过UI:File->Project Structure->(Libraries or Modules/Dependencies tab)

  • Tomcat的嵌入核心
  • Tomcat的嵌入-EL
  • Tomcat的嵌入-的WebSocket
  • Tomcat的嵌入测井,巨力

Maven有mvn dependency:get命令下载任意罐子到当地回购。 Gradle可以有相同的

UPD。对于maven的情况。不要在Idea中添加模块依赖项,最好声明编译范围为spring-boot-starter-tomcat lib的maven配置文件。

+1

你的更新解决了这个问题,**但是**当你创建一个战争文件(生产arctifact)时,你必须记住你必须激活配置文件。这不会自动为您完成。 – eav

+0

如果您的制作工件(PA)需要在内部使用嵌入式tomcat(ET)(在.war中),则不需要将_spring-boot-starter-tomcat_声明为_provided_ dependency。我的答案解释了当我们需要带** no ** ET的PA时(因为它必须部署在某个容器本身),并且想要在IntelliJ中使用** embed-tomcat在本地启动'SpringBootApplication' **,配置。 –

+1

我认为这个更新对我来说是这个问题的正确解决方案:您可以在没有嵌入式Tomcat的情况下构建战争,并且可以使用IntelliJ中的配置文件。非常优雅,不需要评论/取消注释 – Peter

0

配置here为我工作:

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" 
}