Home NoClassDefFoundError:javax/servlet/ServletOutputStream报错解决
Post
Cancel

NoClassDefFoundError:javax/servlet/ServletOutputStream报错解决

报错信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
java.lang.NoClassDefFoundError: javax/servlet/ServletOutputStream
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletOutputStream
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main"

报错原因

1、test范围:指的是测试范围有效,在编译和打包时都不会使用这个依赖。
2、compile范围:指的是编译范围有效,在编译和打包时都会将依赖存储进去。
3、provided范围:在编译和测试的过程有效,最后生成war包时不会加入,诸如:servlet-api,因为servlet-api,Tomcat等web服务器已经存在了,如果再打包会冲突。
4、runtime范围:在运行的时候依赖,在编译的时候不依赖。

解决办法

pom.xml中引入的servlet-api中定义了provided,把这里的provided改成compile

1
2
3
4
5
6
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
</dependency>
1
2
3
4
5
6
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>compile</scope>
</dependency>

参考:
java javax.servlet_java.lang.NoClassDefFoundError: javax/servlet/ServletOutputStream 报错解决
Maven中servlet-api的dependency的scope作用域为什么是provided?

This post is licensed under CC BY 4.0 by the author.