0

I type mvn -v into the terminal and it gives me

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)

The java version I am using is the current Java 8, my $JAVA_HOME variable is set to /Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home.

java -v gives me

java version "1.8.0_102"
Java(TM) SE Runtime Environment (build 1.8.0_102-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)

so that should all be correct, right? Any idea what I might be missing here?

EDIT: As it turns out, the export JAVA_HOME line was missing from my .bash_profile. Fixed thanks to the correct answer below.

BlueTooth4269
  • 113
  • 3
  • 14
  • There's only one possible reason for this error: you are using an older version of Java. A Java version other than Java 8 is being picked up somehow on your system. – Jesper Mar 20 '17 at 11:34
  • 51 means it was built for 1.7 so as previous answer indicates, you have a mismatch of Java-version. – 7663233 Mar 20 '17 at 11:43
  • 1
    Possible duplicate of [Maven Installation OSX Error Unsupported major.minor version 51.0](http://stackoverflow.com/questions/29255495/maven-installation-osx-error-unsupported-major-minor-version-51-0) – l'L'l Mar 20 '17 at 11:49
  • Have you set M2_HOME somewhere? If so remove it ... – khmarbaise Mar 20 '17 at 11:53
  • Running `mvn --version` will tell you what version of not only Maven you have, but, also, what version of Java Maven finds on the `PATH`. – carlspring Mar 20 '17 at 11:55
  • 1
    @carlspring No it won't, not if it first crashes with `UnsupportedClassVersionError`. The questioner actually did run `mvn -v` which is a synonym for `mvn --version`. – JeremyP Mar 20 '17 at 12:19
  • @JeremyP: You are right. In my mind, I seem to have confused `-v` with `-X` (thinking it stood for verbose, like many other tools do). – carlspring Mar 20 '17 at 12:28

2 Answers2

3

First of all, the exception is because the library being loaded is too new for the version of Java running. Version 51.0 is Java 7, so mvn must be running with Java 6 or earlier. This is not up for dispute, no matter what you think JAVA_HOME is.

The command mvn is a shell script that executes the mvn Java executable. It's quite short and easily understandable.

You can confirm the version of Java running by inserting

echo "$JAVA_HOME"
echo "$JAVACMD"
"$JAVACMD" -version

just before the line at the end that starts with

exec "$JAVACMD" ....

It's probably going to Apple's version of java in /System/Library, you almost certainly don't have JAVA_HOME set completely correctly.

EDIT

From the comments, it seems that mvn thinks $JAVA_HOME is

/System/Library/Frameworks/JavaVM.framework/Versions/Current‌​JDK/Home 

This is the location of the Apple JDK which is based on Java 1.6. The correct way to fix the issue is to make sure that JAVA_HOME is set correctly in the right .profile or .bash_profile depending on which shell you use.

The following is my .profile with all non mvn related bits removed.

 JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8/Contents/Home
 M2_HOME=/usr/local/maven
 M2=$M2_HOME/bin
 MAVEN_OPTS="-Xmx512M"
 export M2_HOME M2 JAVA_HOME MAVEN_OPTS

 PATH=$JAVA_HOME/bin:$HOME/bin:$PATH:$M2

 EDITOR=vim 
 export EDITOR
JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • Absolutely correct. That did confirm it's using Java 6. How would I go about sorting that out? I'm hardly an expert on these matters. – BlueTooth4269 Mar 20 '17 at 13:12
  • @BlueTooth4269 What value of JAVA_HOME did it print out?. – JeremyP Mar 20 '17 at 13:39
  • `/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home /System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home/bin/java java version "1.6.0_65" Java(TM) SE Runtime Environment (build 1.6.0_65-b14-468-11M4833) Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-468, mixed mode)` – BlueTooth4269 Mar 20 '17 at 13:44
  • So JAVA_HOME is set to the Apple JDK. Set it correctly before running mvn. – JeremyP Mar 20 '17 at 13:50
  • How would I do that? Typing `echo $ JAVA_HOME` in the terminal gives me `/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home`. – BlueTooth4269 Mar 20 '17 at 13:59
  • @BlueTooth4269 Also check thatthe mvn script is not setting it. – JeremyP Mar 20 '17 at 14:01
  • 1
    Thank you very much, that solved it - I was missing the export line in my .bash_profile. Added it and it works - brilliant. – BlueTooth4269 Mar 20 '17 at 14:03
0

Can you please check if /apache-maven/bin/mvn(incase of linux/unix) or /apache-maven/bin/mvn.cmd(in case of windows) file in apache maven installation has JAVA_HOME set to some old version of Java.

Its possible that its using the old java version.

ProgrammerBoy
  • 876
  • 6
  • 19