Add to Google Reader or Homepage

Reason for java.lang.ClassFormatError

Class Format Error:

Here,i am going to present what i have learnt when dealing with byte codes in the class file.We all know that whenever a java program is successfully compiled,the class file would be generated.Then you may think how come this ClassFormatError is thrown at run-time.

So let's discuss about this error in detail  to know the reasons behind them.

In the java API documentation it is given that "this error would be thrown when the java virtual machine attempts to read a class file and founds that the class file has been altered in such a way that it loses its integrity."

Here they are trying to say that "if a class file has been modified in a complicated way  then this error would be thrown." 

There may be two possible ways a class file can get corrupted as far i know.

1.May be the compiler have some unresolved bugs in it.

2.The programmer may intentionally change the class file for some reasons.

You may ask,why am i gonna change a class file? after all it is compiled(and executed) successfully.The fact is that when you want to develop a state-of-the-art application then you must have to deal with byte codes.

So here you have to note an important thing that "this error will occur either if someone intentionally modified the class file or your compiler have some bugs in it."

I will show you know now how come this error occurs,Before that i want to remind you about the Hex editors.The Hex editors are applications which lets you edit the file that contains the hex codes. Since the class files contains Hex codes,we can able to edit it.

I have included a simple program that performs the addition of two numbers and i edited the class file using the hex editor to show you how this error occurs.

Program:

import java.io.*;
import java.util.*;
public class Add
{
public static void main(String args[])
{
int a;
int b;
a=1;
b=2;
int c=a+b;
System.out.println("The value is:"+c);
}
}

Have a look at the Hex codes contained in the class file generated for this program.






Consider the line 0C0: 01 00 0D 54 68 65 20 76 in which the second pair of Bytes contains 00(NOP) which means No operation. For the sake of optimizing the program i have changed the value 00 as 1D which loads a integer value from the local variable.







But doing so,causes java.lang.ClassFormatError to be thrown during run-time as follows:




The change that, i have made results in the truncation of the class file......

Thus while dealing with byte codes in the class file,ensure that you do not try to change the byte codes unnecessarily.Note that,even if you try to change the particular byte codes then you would not get this error,but your ultimate output would get changed.

So,while changing the byte codes be conscious about the consequences of your modification.




1 comments:

Kaparthicynixit said...

Nice Post Thanku For Sharing Sch a wonderful Post
Java Training
Visit us: java course

Post a Comment

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