问题
I have a Maven Eclipse application which I'm trying to run through the command prompt using the following command:
java -cp target/classes;"target/dependency/*" com.example.Main
Unfortunately, it throws an error:
UnsupportedClassVersionError : unsupported major.minor version 51.0
I did some digging and found out it has something to do with the application being compiled on a JDK of a newer version, than the JRE it runs on. Apparently the 51 means it was built for 1.7, but I don't have a JDK 7 on my computer...
So I made sure that everything is set to version 1.6 :
system.properties file in the root contains
java.runtime.version=1.6
Added the maven property
<java.version>1.6</java.version>
Specified the maven compiler plugin in my pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
I checked the version of my java using the command java -version which returned
Java version "1.6.0_38"
Java(TM) SE Runtime Environment (build 1.6.0_38-b05)
Java HotSpot(TM) 64-bit Server VM (build 20.13-b02, mixed mode)
And an update of my project after setting the maven-compiler-plugin resulted in Eclipse automatically setting the compiler level to 1.6, the project facet level to 1.6 and adding the JavaSE-1.6 jre system library to my project.
So I have absolutely no idea where this JDK 1.7 is coming from. I never installed JDK 1.7 on this computer to begin with...
Also, originally, the maven-compiler-plugin wasn't present so it was build with 1.5 I guess. But I did a mvn clean install
, then a mvn package
, and then tried the command again after all the settings were adjusted and it still throws the same error.
回答1:
Probably you have a dependency that needs Java 1.7
回答2:
You're probably using a dependency which requires Java SE 7.
If this is the case either upgrade to Java SE 7 (recommended if you're using the JRE from Oracle without support contract, as Java SE 6 reached End of Life in February) or replace this dependency.
来源:https://stackoverflow.com/questions/15567514/unsupported-major-minor-version-51-0-but-everything-is-set-to-jdk-1-6