本文轉自https://blog.csdn.net/lslk9898/article/details/73836745
近日有同事遇到在編譯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目錄也是最好的選擇.
步驟:
然后再編譯運行項目即可.