javax/servlet/ServletOutputStream
异常信息:
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:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
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"
Process finished with exit code 1
解决办法:
当遇到这个异常的时候,首先判断是缺少了包,然后就在maven官网搜索 javax/servlet/ServletOutputStream的包进行导入
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
可是导入后发现还是不行,依然报错,几番周折,也许开始不认识下面这个标签吧,就尝试的删除了下面的这个标签,然后就可以了
<scope>provided</scope>
provided适合在编译和测试的环境,他和compile很接近,但是provide仅仅需要在编译和测试阶段,同样provide将不会被打包到lib目录下。
结论:
现在能给出的判断结果是,在使用main方法进行测试的时候,缺少javax/servlet/ServletOutputStream包,然后进行导入后,忘记去掉provided这个标签,因为main方法进行测试所处于的环境应该不属于编译或者测试环境.
在使用main方法测试完成后记得加上provided标签,或者将pom文件中引入的这个包给注释掉,否则tomcat插件不能启动,报的异常是
Caused by: java.lang.ClassCastException: org.springframework.web.SpringServletContainerInitializer cannot be cast to javax.servlet.ServletContainerInitializer
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[]]