Add to Google Reader or Homepage

java.lang.unsupportedclassversionerror

 UnsupportedClassVersionError:


             This Unsupported class version error is one of the common exceptions in java programming. It belongs to java.lang.package.In this post i am going to discuss about this UnsupportedClassVersion error in detail.

What is meant by Unsupported Class Version error?

      As the name indicates that the class file that you are trying to execute is not supported by this Java Virtual Machine.You may wonder why this class file is not supported? after all this is a class file containing the byte codes.

      Before going to discuss about this exception let us have a glance at the file format of the class file.We all know that the class file is formed when we compile the program..

     This class file consists of a two important version numbers. 

1.Minor version number
2.Major version number

These two version numbers decide whether this class file is supported by this virtual machine or not.

When this Unsupported Class Version Error is thrown?

      When trying to execute the class file,java interpreter first reads the magic numbers 0x CA FE BA BE   which identifies whether it is a class file or not,immediately after this magic number the version numbers are located.When the Java Virtual Machine identifies that the virtual numbers in the class file are different from what it can be able to support, then this exception is thrown.

       This exception mainly occurs if you compile the program using the Java virtual Machine of higher version and trying to execute in the JVM of lower version.

 

 How to fix this Exception?


 If there is a way available to convert your class file from higher version to lower version then your problem might be solved.Fortunately a jar package had already been developed for this purpose,which is known as "Retroweaver"

This Retroweaver enables you to convert a class file from higher version to lower version.But one of the disadvantage of this jar file is it enables you to convert only class files created using jdk 1.5 to its lower version.It does not support class files generated using jdk1.6 and other higher versions.

Search for the keyword Retroweaver in Google and download it from available links.After downloading it extract  the  file using Winzip .There you can see a folder "release" which lists some jar files.The jar file retroweaver-ex.jar provides you the graphical interface for converting the class files.

    If you use jdk 1.6 for developing your programs, I suggest you to use the same JVM  to execute the program which You used to compile it.I guess this will fix the problem.
   
You can also download the desired JVM  that supports  the class file to be executed from the internet and install it.

    
To find the class version number that your JVM supports use the following program.

Program:


import java.lang.*;
import java.util.Properties;
import java.io.*;

public class Prop
{
public static void main(String args[])
{
System.out.println(System.getProperty("java.class.version"));
}
}
Output:

50.0

Here,
Major version number: 50
Minor Version number:0

To find the version number of the class file that you are trying to execute, use the following program and supply the name of the class file as input to the program.

Program:

import java.io.*;
import java.util.*;

public class Fileclassread
{
public static void main(String args[])
{
String line;

try
{
DataInputStream in=new DataInputStream(new FileInputStream("Child1.class"));
int bytesavailable=in.available();

for(int i=0;i
{
if(in.readInt()==0xCAFEBABE)
{
System.out.println(in.readShort());
System.out.println(in.readShort());
}
}
}
catch(EOFException e)
{
System.out.println("the file has reached its end...");
}
catch(IOException e)
{
e.printStackTrace();
}
}
}

The output of the above program is as follows:

The minor version number:0
The major version number:50
the file has reached its end...


 
java errors and exceptions © 2010 | Designed by Chica Blogger | Back to top