Search code examples
mavenmaven-compiler-plugintools.jar

How to get maven compiler plugin to work without failing with tools.jar not found error?


We are having some problems using maven under a strange unix-ish dev platform. The maven compiler plugin complains about not being able to find the tools.jar due to weird JDK/JRE confusion in the default installed java.

[ERROR] 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: our-java-home-dir/../lib/tools.jar -> [Help 1]

The ../lib/tools.jar should really be lib/tools.jar. The java installation (Java 7) seems to be a JDK but somehow java is detecting it as only a JRE, hence the confusion. In looking at what the java code is doing, it must be thinking that we have a JRE installation and is expecting JAVA_HOME to be pointing to the jre subdir of the real JAVA_HOME. No amount of copying, symlinking, or other hackery seemed to work for us to get ../lib to be the same as lib.

We've seen a lot of similar questions on the intertubes but none were of much help. We were not able to reinstall Java or any solution like you would do on other platforms but it is supposed to be a full JDK and includes all of the j*tools down in the bin subdirectory.


Solution

  • ... tools.jar not found: our-java-home-dir/../lib/tools.jar -> [Help 1]

    One solution for us that seems to work was is to the force the tools.jar file onto the boot-class-path of the maven command by hand. We used the following program to dump the current value of the boot-class-path:

    public class Main {
        public static void main(String[] args) {
            System.out.println("Boot-class-path=" +
                System.getProperty("sun.boot.class.path"));
        }
    }
    

    Then we modified the end of the mvn script in $M2_HOME/bin/mvn to insert the -Xbootclasspath option:

    $MAVEN_OPTS \
        -Xbootclasspath:existing-boot-class-path-here:${JAVA_HOME}/lib/tools.jar \
        -additional-maven-options-go-here...
    

    We added the tools.jar entry to the end of the value spat out by the main program above. There are other maven options that you need to leave in there of course.

    Hope this helps someone else.