Tuesday, November 18, 2014

Exception in thread "main" java.lang.UnsupportedClassVersionError: AdapterTest : Unsupported major.minor version 51.0


Exception in thread "main" java.lang.UnsupportedClassVersionError: AdapterTest : Unsupported major.minor version 51.0

Description and Solving:
First check the javac and java version:

rizvi@rizvi-pc:~/Desktop/Adapter$ javac -version
javac 1.7.0_65
rizvi@rizvi-pc:~/Desktop/Adapter$ java -version
java version "1.6.0_32"
Java(TM) SE Runtime Environment (build 1.6.0_32-b05)
Java HotSpot(TM) 64-Bit Server VM (build 20.7-b02, mixed mode)
rizvi@rizvi-pc:~/Desktop/Adapter$

So, a higher JDK version  is used to compile the source file and  a lower JDK version is used to run the program.
But its important to note is that vice-versa is not true "you can compile your program in J2SE 1.4 and run on J2SE 1.5 and you will not get any UnSupportedClassVersionError". When a higher JDK is used for compilation it creates class file with higher version and when a lower JDK is used to run the program it found that higher version of class file not supported at JVM level and results in java.lang.UnsupportedClassVersionError.

First solution:

The version number "major.minor version 51.0" show which Java was used to run eclipse.
51.0
= Java J2SE 7
To fix your problem you should try to run eclise with Java 7. This can be done by updating your Java configuration:
sudo update-alternatives --config java
Then select a Java 7 version and retry.
If Java 7 isn't installed you can get it with apt-get:
sudo apt-get install default-jdk

------------------------------------------------------------------------------

Second Solution:

http://crunchify.com/exception-in-thread-main-java-lang-unsupportedclassversionerror-comcrunchifymain-unsupported-major-minor-version-51-0/

------------------------------------------------------------------------------

Third Solution: 

Java Cross-compilation:
rizvi@rizvi-pc:~/Desktop/Adapter$ javac -target 1.6 -source 1.6 -bootclasspath /usr/local/java/jdk1.6.0_32/jre/lib/rt.jar *.java

Here,
target and source must be same.
rizvi@rizvi-pc:~/Desktop/Adapter$ java AdapterTest;
Area
= 100
rizvi@rizvi-pc:~/Desktop/Adapter$

No comments:

Post a Comment