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包,然后進行導入后,忘記去掉
在使用main方法測試完成后記得加上
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[]]
