Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maven not working with Java on Mac OS X

Tags:

java

macos

maven

I downloaded the latest version of JDK8 for Mac OS X and also installed maven. However, I seem to be having an incompatibility between Java and maven:

myusername$ mvn -version
    Exception in thread "main" java.lang.UnsupportedClassVersionError: org/apache/maven/cli/MavenCli : Unsupported major.minor version 51.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(ClassLoader.java:637)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
        at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClassFromSelf(ClassRealm.java:401)
        at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:42)
        at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass(ClassRealm.java:271)
        at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:254)
        at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:239)
        at org.codehaus.plexus.classworlds.launcher.Launcher.getMainClass(Launcher.java:144)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:266)
        at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
        at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
        at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)

I am using:

myusername$ javac -version
javac 1.8.0_45

How can I make Java and maven compatible with each other?

Tried instructions from possible duplicate question: Maven Installation OSX Error Unsupported major.minor version 51.0:

myusername$ export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.6.0_65.jdk/Contents/Home
myusername$ mvn -version
Error: JAVA_HOME is not defined correctly.
  We cannot execute /Library/Java/JavaVirtualMachines/jdk1.6.0_65.jdk/Contents/Home/bin/java
like image 472
warship Avatar asked Jan 09 '23 04:01

warship


2 Answers

The JAVA_HOME of 1.6.x Java versions on the Mac is /Library/Java/Home only Java 7 and higher are installed below /Library/Java/JavaVirtualMachines

Execute /usr/libexec/java_home -version 1.8 to get the current 1.8 Java Home. I use export JAVA_HOME=$(/usr/libexec/java_home -version 1.8) to set my correct JAVA_HOME

like image 162
mp911de Avatar answered Jan 15 '23 11:01

mp911de


I was getting the same exact error, no matter what JVM target I chose. The solution was to downgrade maven:

brew uninstall maven
brew tap homebrew/versions
brew install maven30
like image 45
benipsen Avatar answered Jan 15 '23 10:01

benipsen