关于Maven项目build时出现No compiler is provided in this environment的处理

近日有同事遇到在编译Maven项目时出现
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
的问题, 原以为这是个个例, 源于同事粗心, 配置环境出问题造成, 后到百度查看一下, 遇到这个问题的不在少数, 但是对问题的解释没有说到根源, 于是写下这篇博客供大家参阅, 如有纰漏, 还请指正.

错误代码节选:

[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.436 s
[INFO] Finished at: 2017-06-28T11:16:07+08:00
[INFO] Final Memory: 10M/151M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project manage: Compilation failure
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
[ERROR] -> [Help 1]

但是编写普通Java Project编译运行却是正常的,下图为只有输出语句的普通java类


从上图中可以看出, java编译环境未jre1.7.0_17, 也就是说并没有配置成jdk目录, 然后看Eclipse-->Window-->preferences-->Java-->Installed JREs


为了演示出效果, 在测试之前, 我已经将系统java环境配置成如上图所示路径, 并只保留该配置, 由下图可以看出, 该路径是我所安装的两个JDK版本中的一个JDK自带的jre运行环境. 使用该环境编译普通项目没有问题, 但为什么会在编译Maven项目时出错呢?


我们看看Maven的环境是如何配置的:先找到Eclipse-->Window-->preferences-->Maven-->Installations


在Maven配置中, 我并没有使用Eclipse自带的Maven插件, 而是重新配置的Maven环境, 然后再看Eclipse-->Window-->preferences-->Maven-->User Settings


Maven设置使用的是Maven中conf文件夹下的settings.xml, 点击"open file" 在Eclipse中查看具体配置信息, 仅摘录与本错误信息相关的部分

  <profiles>
    <!-- profile
     | Specifies a set of introductions to the build process, to be activated using one or more of the
     | mechanisms described above. For inheritance purposes, and to activate profiles via <activatedProfiles/>
     | or the command line, profiles have to have an ID that is unique.
     |
     | An encouraged best practice for profile identification is to use a consistent naming convention
     | for profiles, such as 'env-dev', 'env-test', 'env-production', 'user-jdcasey', 'user-brett', etc.
     | This will make it more intuitive to understand what the set of introduced profiles is attempting
     | to accomplish, particularly when you only have a list of profile id's for debug.
     |
     | This profile example uses the JDK version to trigger activation, and provides a JDK-specific repo.-->
    
    <profile>
    	<id>jdk-1.7</id>
    	<activation>
    		<activeByDefault>true</activeByDefault>
    		<jdk>1.7</jdk>
    	</activation>
    	<properties>
			<maven.compiler.source>1.7</maven.compiler.source>
			<maven.compiler.target>1.7</maven.compiler.target>
			<maven.compiler.compilerVersion>1.7</maven.compiler.compilerVersion>
		</properties>
    </profile>
  </profiles>

中间具体信息的理解, 可以参见  冰河winner 的博客. 也就是说, 根据上面的配置, 我们需要指定一个符合配置的JDK环境, 这是我们之前在Eclipse-->Window-->preferences-->Java-->Installed JREs下的配置就不行了, 而需要指定一个JDK目录, 例如我的JDK安装目录下的jdk1.7.0_17, 这也是这个错误出现的罪魁祸首. 不过对于Java开发者来说, Installed JREs中使用jdk目录而不适用jre目录也是最好的选择.


步骤:








然后再编译运行项目即可.

  • 284
    点赞
  • 543
    收藏
    觉得还不错? 一键收藏
  • 153
    评论
"No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?"这个错误提示意味着在执行maven install命令,没有提供编译器。这通常是因为你的环境中只安装了JRE(Java Runtime Environment),而没有安装JDK(Java Development Kit)。 要解决这个问题,你需要确保在你的环境中安装了JDK,并且配置了正确的JAVA_HOME环境变量。 以下是解决这个问题的步骤: 1. 首先,确保你已经安装了JDK。你可以在命令行中运行`java -version`命令来检查JDK是否正确安装并显示版本信息。 2. 如果你没有安装JDK,你需要下载并安装适合你操作系统的JDK版本。你可以从Oracle官方网站下载JDK安装程序。 3. 安装完JDK后,你需要配置JAVA_HOME环境变量。在Windows系统中,你可以按照以下步骤进行配置: - 右键点击“我的电脑”或“此电脑”,选择“属性”。 - 在弹出的窗口中,点击“高级系统设置”。 - 在系统属性窗口中,点击“环境变量”按钮。 - 在系统变量中,点击“新建”按钮。 - 输入变量名为JAVA_HOME,变量值为JDK的安装路径(例如:C:\Program Files\Java\jdk1.8.0_221)。 - 点击“确定”保存配置。 4. 配置完JAVA_HOME后,你还需要将JDK的bin目录添加到系统的Path环境变量中。在系统变量中找到Path变量,点击“编辑”按钮,在变量值的末尾添加JDK的bin目录路径(例如:C:\Program Files\Java\jdk1.8.0_221\bin),多个路径之间用分号分隔。 5. 保存配置并关闭所有命令行窗口。重新打开一个新的命令行窗口,运行`java -version`命令来验证JDK是否正确配置。 现在,当你再次执行maven install命令,应该不会再出现"No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?"的错误提示了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 153
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值