1. 问题描述
使用idea对Java工程执行mvn compile
命令进行编译,出现以下报错:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.5:compile (default-compile) on project test: Compilation failure
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
2. 问题分析
从报错信息上看,是由于环境没有提供编译器,可能编译过程运行在JRE上,而不是JDK上。
查看了idea项目配置的Java编译器为JDK8,且配置的Java路径正确。
执行命令mvn -v
,查看maven的配置。发现runtime:
的路径有些奇怪。
Apache Maven 3.8.3 (ff8e977a158738155dc465c6a97ffaf31982d739)
Maven home: /usr/local/apache-maven-3.8.3
Java version: 1.8.0_311, vendor: Oracle Corporation, runtime: /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
Default locale: zh_CN, platform encoding: UTF-8
OS name: "mac os x", version: "10.16", arch: "x86_64", family: "mac"
3. 解决方案
从网上搜索了解决方案,发现这篇文件介绍的比较靠谱。
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
采用了文章中的方法3,重新设置JAVA_HOME
路径。
- 添加Java环境变量
# cat ~/.bash_profile
export M2_HOME=/usr/local/apache-maven-3.8.3
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_311.jdk/Contents/Home
export PATH=$PATH:$M2_HOME/bin:$JAVA_HOME/bin
- 使环境变量生效
source ~/.bash_profile
- 查看maven配置
# mvn -v
Apache Maven 3.8.3 (ff8e977a158738155dc465c6a97ffaf31982d739)
Maven home: /usr/local/apache-maven-3.8.3
Java version: 1.8.0_311, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_311.jdk/Contents/Home/jre
Default locale: zh_CN, platform encoding: UTF-8
OS name: "mac os x", version: "10.16", arch: "x86_64", family: "mac"
发现maven的runtime:
已经显示jdk1.8.0_311.jdk,运行环境已经正常。
此时再次执行编译命令mvn compile
,发现Java工程可以正常编译,不在出现报错。