简体   繁体   中英

Builds failing after upgrading to Java7, Missing Tools.jar and bad class versions

I recently upgraded to Java7 on my Macbook Pro. I downloaded the JDK (not the JRE).

» javac version
javac 1.7.0_17

» echo $JAVA_HOME    
/Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home

However, when trying to run a build, one of the maven compiler plugins is failing, claiming I have a JRE installed:

» mvn install
[ERROR] execute error
org.apache.maven.plugin.MojoExecutionException: You need to run build with JDK
 or have tools.jar on the classpath.
If this occures during eclipse build make sure you run eclipse under JDK as well
    at com.mysema.maven.apt.AbstractProcessorMojo.execute(AbstractProcessorMojo.java:263)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)

I'm confused, as I clearly have a JDK installed. My MAVEN_OPTS don't do anything funny:

» echo $MAVEN_OPTS          
-Xmx512m

Trying to debug, I checked the source of the plugin in question, which is doing the following:

    try {
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        if (compiler == null) {
            throw new MojoExecutionException("You need to run build with JDK or have tools.jar on the classpath."
                    + "If this occures during eclipse build make sure you run eclipse under JDK as well");
        }

That seems harmless, so I suspected something wrong on my command line env, and wrote a simple test:

// Main.java
import javax.tools.JavaCompiler;
import javax.tools.ToolProvider;


public class Main {

    public static void main(String[] args) {
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        if (compiler == null)
        {
            System.out.print("Compiler is null");
        } else {
            System.out.print("Compiler is not null");
        }
    }

}

» javac Main.java

Main.java:1: cannot access javax.tools.JavaCompiler
bad class file: /Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home/jre/lib/rt.jar(javax/tools/JavaCompiler.class)
class file has wrong version 51.0, should be 49.0
Please remove or make sure it appears in the correct subdirectory of the classpath.
import javax.tools.JavaCompiler;
                   ^
1 error

If I understand that error correctly, it suggests a Java7 compiler running against a Java5 rt.jar ?

I'm not sure what's going on here.

I clearly have a Java7 JDK installed, but don't understand:

  • Why is the maven plugin is returning null for the compiler?
  • Why am I getting class version errors?

Update

Answers are suggesting a messed up JRE / JDK install. I agree this seems likely, but having trouble tracking down where the culprit lies.

Some additional info:

» which javac
/usr/bin/javac

ls -ltra /usr/bin/javac
/usr/bin/javac -> /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/javac

cd /System/Library/Frameworks/JavaVM.framework/Versions
ls -ltra

lrwxr-xr-x   1 root  wheel   10 18 Feb 08:36 1.6.0 -> CurrentJDK
lrwxr-xr-x   1 root  wheel   10 18 Feb 08:36 1.6 -> CurrentJDK
lrwxr-xr-x   1 root  wheel   10 18 Feb 08:36 1.5.0 -> CurrentJDK
lrwxr-xr-x   1 root  wheel   10 18 Feb 08:36 1.5 -> CurrentJDK
lrwxr-xr-x   1 root  wheel   10 18 Feb 08:36 1.4.2 -> CurrentJDK
lrwxr-xr-x   1 root  wheel   10 18 Feb 08:36 1.4 -> CurrentJDK
drwxr-xr-x   8 root  wheel  272 18 Feb 08:39 A
drwxr-xr-x  11 root  wheel  374 18 Feb 08:39 ..
lrwxr-xr-x   1 root  wheel    1 14 Jun 11:14 Current -> A
lrwxr-xr-x   1 root  wheel   58 14 Jun 11:15 CurrentJDK -> /Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents
drwxr-xr-x  11 root  wheel  374 14 Jun 11:15 .

So, the javac that is running is the one installed in A/Commands . I'm not sure if this is correct or not. It seems wrong from my windows days, but I'm not familiar enough with how Mac treats Java install to go tinkering.

My $JAVA_HOME points to /Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents/Home , which has the following:

drwxrwxr-x  10 root  wheel       340  5 Feb 08:10 jre
-rw-rw-r--   1 root  wheel    123324  5 Feb 08:10 THIRDPARTYLICENSEREADME-JAVAFX.txt
drwxrwxr-x   5 root  wheel       170  5 Feb 08:10 man
drwxrwxr-x   9 root  wheel       306  2 Mar 02:10 db
-rw-rw-r--   1 root  wheel      3339  2 Mar 02:10 COPYRIGHT
drwxrwxr-x   9 root  wheel       306  2 Mar 02:10 include
-rw-rw-r--   1 root  wheel  19997030  2 Mar 02:10 src.zip
-rw-rw-r--   1 root  wheel       447  2 Mar 02:10 release
-rw-rw-r--   1 root  wheel    172252  2 Mar 02:10 THIRDPARTYLICENSEREADME.txt
-rw-rw-r--   1 root  wheel       114  2 Mar 02:10 README.html
-rw-rw-r--   1 root  wheel        40  2 Mar 02:10 LICENSE
drwxrwxr-x   5 root  wheel       170  2 Mar 02:10 ..
drwxrwxr-x  15 root  wheel       510  2 Mar 02:13 .
drwxrwxr-x  13 root  wheel       442  2 Mar 02:13 lib
drwxrwxr-x  43 root  wheel      1462  2 Mar 02:13 bin

I found a tools.jar sitting in /Library/Java/Extensions

I'm not sure if this is standard or not.

However, copying the tools.jar from the JDK7 $JAVA_HOME/lib to /Library/Java/Extensions solved all my problems.

I should also point out that in my original question I had updated the Java CurrentSDK to point to JDK7 :

CurrentJDK -> /Library/Java/JavaVirtualMachines/jdk1.7.0_17.jdk/Contents

This was a Bad Idea, as pointed out here .

I updated this to point back to 1.6. Although this seems counter-intuitive, it was required to get things working.

The current listing of /System/Library/Frameworks/JavaVM.framework/Versions with a 1.7 JDK looks as follows:

lrwxr-xr-x   1 root  wheel   10 18 Feb 08:36 1.6.0 -> CurrentJDK
lrwxr-xr-x   1 root  wheel   10 18 Feb 08:36 1.6 -> CurrentJDK
lrwxr-xr-x   1 root  wheel   10 18 Feb 08:36 1.5.0 -> CurrentJDK
lrwxr-xr-x   1 root  wheel   10 18 Feb 08:36 1.5 -> CurrentJDK
lrwxr-xr-x   1 root  wheel   10 18 Feb 08:36 1.4.2 -> CurrentJDK
lrwxr-xr-x   1 root  wheel   10 18 Feb 08:36 1.4 -> CurrentJDK
drwxr-xr-x   8 root  wheel  272 18 Feb 08:39 A
drwxr-xr-x  11 root  wheel  374 18 Feb 08:39 ..
lrwxr-xr-x   1 root  wheel    1 14 Jun 11:35 Current -> A
lrwxr-xr-x   1 root  wheel   59 14 Jun 12:31 CurrentJDK -> /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents
drwxr-xr-x  11 root  wheel  374 14 Jun 12:31 .

The correct way to set JAVA_HOME on MacOS X is to use:

[user@ip ~]$ export JAVA_HOME=$(/usr/libexec/java_home -v1.7)
[user@ip ~]$ echo $JAVA_HOME 
/Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents/Home

Your java installation is broken if, after doing this, $JAVA_HOME/lib does not contain a tools.jar.

Tip: I have these two aliases specified in my ~/.bash_profile:

alias java6="export JAVA_HOME=$(/usr/libexec/java_home -v1.6)"  
alias java7="export JAVA_HOME=$(/usr/libexec/java_home -v1.7)"

Placing jars in /Library/Java/Extensions is equivalent to putting them in the global classpath and is rarely a good idea.

Forget Maven.

What directory has the JDK /bin directory? That's JAVA_HOME .

I suspect that you've set the environment variable incorrectly.

And that /bin ought to have all the executables for the JDK tools: javac, java, jar, javap, etc.

If that's not the case, then you don't have JDK, you've got JRE.

I've got a Windows machine. Here's where I have JDK installed:

C:\Program Files\Java\jdk1.7.0_17

And here's JAVA_HOME:

JAVA_HOME=C:/Program Files/Java/jdk1.7.0_02

No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?

Maven build failure in eclipse with above error was fixed by un-installing older version of java.

Most probably javac is running from JRE and not JDK. tools.jar is only available in JDK. Confirm the path of javac/java by using which

which javac

Drag correct Java7 version to top in Java Preferences (Launchapad/Others). That should point A to JDK7.

I had a similar issue and found the answer in another thread:

Symbolic link messed up on OSX while trying to change the Java version

I set JAVA_HOME as follows:

export JAVA_HOME=/Library/Java/Home

and it's working now.

If you find that the tools.jar in /Library/Java/Extensions is not from the right version of the JDK, then move it out of the way, so that there is no tools.jar there and there is a tools.jar in $JAVA_HOME/lib (as set in the previous response).

eg

cd 
sudo mv /Library/Java/Extensions/tools.jar tools.jar.safe

I got the following error on a maven project: Failed to execute goal on project cobertura-maven-plugin: Could not resolve dependencies for project org.codehaus.mojo:cobertura-maven-plugin:maven-plugin:2.6: Could not find artifact com.sun:tools:jar:0 at specified path /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/../lib/tools.jar -> [Help 1] org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal on project cobertura-maven-plugin: Could not resolve dependencies for project org.codehaus.mojo:cobertura-maven-plugin:maven-plugin:2.6: Could not find artifact com.sun:tools:jar:0 at specified path /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home/../lib/tools.jar

I fixed it by creating a lib dir under /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents and adding tools.jar to it.

Full path would be this for tools.jar in this particular instance: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/lib/tools.jar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM