查看: 806|回复: 0

[Java代码] Java编译器用maven打war包出错解决办法

发表于 2018-4-14 08:00:02

在用maven打包时,出现过如下两个错误:

错误1:程序包javax.servlet不存在,程序包javax.servlet.http不存在

错误2:No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK? Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile)。。。

一、错误1 出现原因:java编译器没有找到软件包javax.servlet。

解决方法:在pom.xml中添加以下依赖包

  1. <dependency>
  2. <groupId>javax.servlet</groupId>
  3. <artifactId>servlet-api</artifactId>
  4. <version>2.5</version>
  5. <scope>provided</scope>
  6. </dependency>
  7. <dependency>
  8. <groupId>javax.servlet</groupId>
  9. <artifactId>javax.servlet-api</artifactId>
  10. <version>3.0.1</version>
  11. <scope>provided</scope>
  12. </dependency>
复制代码

待依赖下载完成后,右键——>maven——>Update Projects…

有时候jsp页面也会报错误,也是相关javax.servlet.http 找不到,一种方法是采用上述方法,在pom中添加依赖;

第二种方法如下:

在项目上右键——>Build Path——>Configure Build Path,然后如图:

依次Next就可以了。

二、错误二

[ERROR] COMPILATION ERROR :
[INFO] ------------------------------------------------------------- [ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK? [INFO] 1 error [INFO] ------------------------------------------------------------- [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.764 s [INFO] Finished at: 2018-03-19T15:14:00+08:00 [INFO] Final Memory: 11M/110M
[INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project book-donation: Compilation failure
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

1.错误原因:eclipse默认选择jre运行程序,而maven的install要求使用jdk,因此报编译环境找不到的错误。

2.解决方法:将jre改为jdk

3.详细步骤:项目右键——>Build Path——>Configure Build Path…,打开窗口后,按照如下图步骤进行选择,此步骤是将jre换为jdk ,将jre换为jdk

步骤完成后,先更新一下maven项目(右键——>maven——>Update Projects…),再clean以下项目(右键——>Run As…

——>Maven Clean),最后再打包(右键——>Run As…——>Maven install)。



回复

使用道具 举报