<

Why am I getting "java.lang.UnsupportedClassVersionError: Arch : Unsupported major.minor version 51.0"

Published on
11,634 Points
5,534 Views
1 Endorsement
Last Modified:
Approved
When compiling a Java source file using javac from JDK 7 the generated class file format is incompatible with previous JRE versions including version 6.

Therefore when trying to run the class file using Java 6 the following cryptic exception is thrown by Java:
Exception in thread "main" java.lang.UnsupportedClassVersionError: HelloWorld: Unsupported major.minor version 51.0

Class version 51.0 is the version of class file generated by javac 7 while Java 6 expects class file format 50.0 or lower.

To solve the problem you need to tell javac 7 to create a class format compatible with previous versions of Java.

With previous versions of java adding the -target flag solved the problem:
javac -target 1.6 HelloWorld.java

However as of Javac 7 this command emits the following error:
javac: target release 1.6 conflicts with default source release 1.7

Adding the -source flag fixes this problem but generates a warning:
javac -source 1.6 -target 1.6 HelloWorld.java
warning: [options] bootstrap class path not set in conjunction with -source 1.6
1 warning


javac is warning us that we are compiling Java source code according to Java 6 source format but linking it with the Java runtime libraries of Java 7.

This works but may lead to subtle runtime bugs.

To fix the warning:
1. Install a Java 6  JRE.
2. Add the -bootclasspath flag to point to the Java 6 rt.jar from the javac 7 command line:
javac -bootclasspath <JRE 1.6 home>\lib\rt.jar -source 1.6 -target 1.6 HelloWorld.java

You should now see that the source compiles without warning and the resulting class file is executable using Java 6.

If you received a class file or a Jar file from a 3rd party and would like to see which class format version is being used, you can use a tool like the jclasslib bytecode viewer

Another concern is that if you are redistributing the JDK together with your product, in this case you'll have to distribute both the JDK 7 and JRE 6 in order for your customers to be able to compile Java 6 compatible class files using a Java 7 javac.
1
Comment
Author:lyaffe
1 Comment
 
LVL 5

Expert Comment

by:Plk_In_EE
very good. thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Join & Write a Comment

Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month