Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fatal error compiling: tools.jar not found - maven-compiler-plugin

Tags:

java

maven

Issue: The following problem occurs when I compile my project w/ maven (note: I'm working on windows):

Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.5.1:compile (default-compile) on project XYZ: Fatal error compiling: tools.jar not found: C:\java\jdk1.8.0_40..\lib\tools.jar -> [Help 1]

Error occurs from both command line and eclipse (Eclipse STS) - mvn clean; mvn install

Notes:

  • System env variable, JAVA_HOME, was set to C:\java\jdk1.8.0_40
  • C:\java\jdk1.8.0_40\lib\tools.jar is there
  • Tried clearing local maven repo (.m2) - error still occurred
  • configured maven compiler plugin to point to C:/java/jdk1.7.0_75/bin/javac.exe executable with source=target=1.7 - same error
  • Tried installing java 1.7.0_75 in eclipse and using that (but maven is still used for the compile -- right-click on project, run as, maven install): same error (even with maven compiler's plugin configuration's executable set to C:/java/jdk1.7.0_75/bin/javac.exe)
like image 776
Tom Dory Avatar asked Mar 24 '15 14:03

Tom Dory


People also ask

How to fix Apache Maven compiler plugin not compiling in Eclipse?

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.

What are the compiler goals of Maven plugin?

Plugin Goals The compiler plugin is used to compile the source code of a Maven project. This plugin has two goals, which are already bound to specific phases of the default lifecycle: compile – compile main source files

How to configure the compiler plugin to work with Java 5?

By default, the compiler plugin compiles source code compatible with Java 5, and the generated classes also work with Java 5 regardless of the JDK in use. We can modify these settings in the configuration element: For convenience, we can set the Java version as properties of the POM: Sometimes we want to pass arguments to the javac compiler.

How to add tools jar in Eclipse?

If tools.jar is not found, try adding it up in your project's dependencies on eclipse. Firstly, copy the location of tools.jar which would look something like this C:\Program Files\Java\jdk1.x.x\lib Then, on eclipse, Right click your project and go to Properties -> Java Build Path -> Libraries Click on ADD External JARs button.


3 Answers

Issue resolved: Setting System env variable, JAVA_HOME, to Java JDK 1.7 fixed the problem.

I tried jdk1.7.0_45 and jdk1.7.0_75 - they both worked.

Notes:

  • I thought setting the compiler executable for the compiler plugin in pom.xml would mean that the executable was used in all cases;
    apparently it's not - the JAVA_HOME variable overrode it in this
    case.
  • Some other folks had issues b/c their JAVA_HOME was set to a JRE instead of a JDK. My JAVA_HOME for Java 1.8 was set to JDK 1.8, but I still received the same error.
like image 140
Tom Dory Avatar answered Sep 26 '22 19:09

Tom Dory


I also faced similar issue when building the project in eclipse ( sts version) I figured out the issue was because I had jre installed in 2 locations and I jre configured in the eclipse was pointing to the pure jre folder. So just check that your jre is pointing to the jre folder inside the jdk not any other jre as the tools.jar is in jdk/lib

like image 29
Liju John Avatar answered Sep 25 '22 19:09

Liju John


Even after configuring Eclipse to use a JDK from Window -> Preferences -> Java -> Installed JRES, you may still get errors when you try to perform a Maven build.

In this case make sure that the JRE tab in Maven Build is configured correctly.

Go to Run -> Run Configurations, open the Maven Build category and do one of the following:

  1. Make sure that the Workspace default JRE is selected and it points to a JDK.
  2. In the Alternate JRE menu, select a configured JDK.

Run Configurations screenshot

like image 27
nkatsar Avatar answered Sep 25 '22 19:09

nkatsar