近日有同事遇到在編譯Maven項目時出現
[ERROR] No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
的問題, 原覺得這是個個例, 源於同事粗心, 配置環境出問題形成, 後到百度查看一下, 遇到這個問題的不在少數, 可是對問題的解釋沒有說到根源, 因而寫下這篇博客供你們參閱, 若有紕漏, 還請指正.
錯誤代碼節選:java
- [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 JREsapache
爲了演示出效果, 在測試以前, 我已經將系統java環境配置成如上圖所示路徑, 並只保留該配置, 由下圖能夠看出, 該路徑是我所安裝的兩個JDK版本中的一個JDK自帶的jre運行環境. 使用該環境編譯普通項目沒有問題, 但爲何會在編譯Maven項目時出錯呢?maven
咱們看看Maven的環境是如何配置的:先找到Eclipse-->Window-->preferences-->Maven-->Installationside
在Maven配置中, 我並無使用Eclipse自帶的Maven插件, 而是從新配置的Maven環境, 而後再看Eclipse-->Window-->preferences-->Maven-->User Settingspost
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目錄也是最好的選擇.
步驟:ui
而後再編譯運行項目便可.this