前言

在Eclipse中使用maven打jar包,遇到异常报错

右击项目Run AsMaven install

或者,右击项目Run AsMaven build....

报错日志

问题1: No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[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: 1.611 s
[INFO] Finished at: 2019-08-26T16:38:43+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project df-nlp: Compilation failure
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

解决方案

根据异常提示显示是JDK的问题,下面几种解决的办法:

方案一

解决方法(1):检查Eclipse是否配置好JDK,Window —- Preferences —- Java —- Installed JREs

如果没有配置的话,就点 右侧的 Add...

注意:Eclipse添加完JDK后,别忘记选择刚添加的JDK就行!!

找的JDK的安装路径

方案二

解决方法(2):先检查电脑本地的环境变量是否配置成功,就是所谓的JAVA_HOME、Path

JDK的安装及配置如下:
JAVA_HOME (JDK安装的路径) : C:\Program Files\Java\jdk1.8.0_201
Path(注意最后面的英文格式分号) : %JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;
CLASSPATH(注意最前面有一点) : .;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar

然后在Eclipse上操作:
右击项目 —- Build Path —- Configure Build Path... —- Java Build Path —- Libraries —- 选中JRE System Library —- Edit —- Workspace default JRE —- 结束

方案三

解决方法(3):修改pom.xml文件
在pom文件中添加一个plugin配置,然后再update下就可以了,前提电脑本地得配置好JDK的环境变量

1
2
3
4
5
6
7
8
9
10
11
12
13
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<fork>true</fork>
<executable>${JAVA_HOME}/bin/javac</executable>
</configuration>
</plugin>
</plugins>
</build>

方案四

解决方法(4):简单最暴力的方法

直接在项目所在文件夹的空白处,按住Shift键鼠标右击,点击 打开命令窗口,然后再命令窗口输入

1
mvn install

用mvn命令打包和Eclipse一样,打完的jar包都是出现在项目的\target目录下