82

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:631)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
    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)

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) 64-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)

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Hassan Syyid
  • 1,559
  • 1
  • 13
  • 24

8 Answers8

108

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.

John Ament
  • 11,595
  • 1
  • 36
  • 45
  • 1
    It was exactly my problem. I have recently installed the mars 2 version of eclipse for 64 bits and new installation used its embedded maven version instead of mine. I have tried two ways: first one was to change de JRE version of the project to 1.8 and the second one was to let the 1.6 JRE and change the maven version from the embedded one to my local installation and both options worked – Facepalmed Jun 02 '16 at 16:03
  • We are using JDE 1.8 and are seeing precisely this problem. – DrLou Mar 24 '17 at 21:24
12

That version number (51.0) indicates that you are trying to run classes compiled for Java 7. You will need to recompile them for Java 6.

Note, however, that some features may no longer be compatible with Java 6, which is very old, and no longer (publicly) supported by Oracle.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
  • 7
    You're funny... most govt here still use java 6, Infact they barely just got there from java 4... went strait from 4 to 6! (and your comment is from last year! :) – VeenarM May 11 '16 at 04:17
5

i also faced similar issue. I could able to solve this by setting JAVA_HOME in Environment variable in windows. Setting JAVA_HOME in batch file is not working in this case.

2

The problem is because you haven't set JDK version properly.You should use jdk 7 for major number 51. Like this:

JAVA_HOME=/usr/java/jdk1.7.0_79

2

I ran into the same problem. I use jdk 1.8 and maven 3.3.9 Once I export JAVA_HOME, I did not see this error. export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/

Dakshin Rajavel
  • 345
  • 2
  • 7
0

I face the same problem and solved by adding the JAVA_HOME variable with updated version of java in my Ubuntu Machine(16.04). if you are using "Apache Maven 3.3.9" You need to upgrade your JAVA_HOME with java7 or more

Step to Do this

1-sudo vim /etc/environment

2-JAVA_HOME=JAVA Installation Directory (MyCase-/opt/dev/jdk1.7.0_45/)

3-Run echo $JAVA_HOME will give the JAVA_HOME set value

4-Now mvn -version will give the desired output

Apache Maven 3.3.9

Maven home: /usr/share/maven

Java version: 1.7.0_45, vendor: Oracle Corporation

Java home: /opt/dev/jdk1.7.0_45/jre

Default locale: en_US, platform encoding: UTF-8

OS name: "linux", version: "4.4.0-36-generic", arch: "amd64", family: "unix"
pintu
  • 331
  • 4
  • 7
0

add jdk8 or higher version at JAVA_HOME and path.

add JAVA_HOME add C:\ProgramFiles\Java\jdk1.8.0_201

For Path =>

add %MAVEN_HOME%\bin

and finally restart your pc.

harun ugur
  • 1,718
  • 18
  • 18
0

As said by John Ament that only maven version 3.2.5 or lower can be used in Java 6. Otherwise, if you want to make the app (in this case, JAR) compatible for Java 6 with maven, you can use Java 8, but set the field java.version or target to 1.6 in the pom.xml file.

If you wanted to create Spring fat jar which is compatible for Java 6, you can add this into your pom.xml:

<properties>
    <java.version>1.6</java.version>
</properties>

As for spring fat jar app, you need to set the tomcat to tomcat-juli:7.0.59 or exclude the whole tomcat or it would raise errors.

Dhana D.
  • 1,670
  • 3
  • 9
  • 33