maven与jdk版本不一致报:Unsupported major.minor version 51.0

时间:2024-08-05 08:07:21

I recently uninstalled Java 8, to use Java 6 as I want my code/creations to be usable by more people than just those on Java 8. When I do mvn - version it returns:

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:)
at java.lang.ClassLoader.defineClass(ClassLoader.java:)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:)
at java.net.URLClassLoader.access$(URLClassLoader.java:)
at java.net.URLClassLoader$.run(URLClassLoader.java:)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClassFromSelf(ClassRealm.java:)
at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:)
at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass(ClassRealm.java:)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:)
at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:)
at org.codehaus.plexus.classworlds.launcher.Launcher.getMainClass(Launcher.java:)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:)

When I do java -version:

java version "1.6.0_45"
Java(TM) SE Runtime Environment (build 1.6.0_45-b06)
Java HotSpot(TM) -Bit Server VM (build 20.45-b01, mixed mode)

Everything seems fine with Java, but this also happens when I try to run executable JARs. I got around it by manually doing java -jar (jar name)

===================

According to maven website, the last version to support Java 6 is 3.2.5, and 3.3 and up use Java 7. My hunch is that you're using Maven 3.3 or higher, and should either upgrade to Java 7 (and set proper source/target attributes in your pom) or downgrade maven.