一、问题
这几天在学习maven,跟着视频上学maven,看到人家直接tomcat:run就成功部署了项目,所以自己也尝试了一下,但是项目可以正常部署,但是访问的时候servlet访问正常。jsp访问出现编译错误。
idea报错如下:
7月 04, 2019 5:00:51 下午 org.apache.jasper.compiler.JDTCompiler$1 findType
严重: Compilation error
org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException
at org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader.<init>(ClassFileReader.java:342)
at org.apache.jasper.compiler.JDTCompiler$1.findType(JDTCompiler.java:206)
at org.apache.jasper.compiler.JDTCompiler$1.findType(JDTCompiler.java:163)
at org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment.askForType(LookupEnvironment.java:96)
二、解决办法:
参考csdn这篇文章解决了Maven中使用tomcat:run出现错误
在pom.xml中添加tomcat7的配置
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>9999</port>
<uriEncoding>UTF-8</uriEncoding>
</configuration>
</plugin>
</plugins>
</build>
使用tomcat7:run运行,servlet可以正常访问
三、思考
那为什么使用 http://localhost:8080/maven_web4/MyServlet 会报错呢?查看页面下方,可以发现,maven默认使用的是Tomcat/6.0.29版本,而我的maven的jdk版本为12.0.1,再查资料发现,有网友说是JRE版本不匹配导致的stackoverflow-unable-to-compile-class-for-jsp。
如果想用maven自带的tomcat6,可以降低jdk版本到1.7解决。我这里的jdk是12版本,使用tomcat7可以解决问题。