When working with Apache Maven in Eclipse, I am convinced that no one didn’t encountered an error as below when building the application:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile (default-compile) on project example: Fatal error compiling: tools.jar not found: C:\Program Files\Java\jre1.8.0_101\..\lib\tools.jar -> [Help 1]
[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] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
What is the cause?
This is because by default Apache Maven will use the Maven Compiler Plugin plugin to compile the code, which will use the JDK tools.jar library, but the default JRE in Eclipse will not contain this library.
So what is the solution to fix this error?
I would like to guide you as follows:
On the Eclipse menu, go to Window -> Preferences.
Select Java and select Installed JREs:
This window displays the default Eclipse JRE,
At this point, you select the default JRE on the right, then click the Edit button, the following window will appear:
In this window, you click the Add External JARs … button to add the JDK tools.jar library.
This library is located in the following directory (on Linux or macOS should be different):
Select the tools.jar file and press the Open button!
Hit the Finish button to close this window!
Click OK to close the Preferences window.
OK, let’s try to build your application again. There will be no more errors!