Add to Google Reader or Homepage

3 reasons for java.lang.verfiyerror

VerifyError:

It is an other sub class of  IncompatibleClassChangeError which is thrown at run-time due to binary incompatibility.If you would like to know what is meant by binary incompatibility, refer to my older post java.lang.IncompatibleClassChangeError.In this post i have explained three reasons upon which this Verify Error is thrown.

Before explaining this error,i would like to tell you something about how a java program gets executed.When ever we compile a program,the byte codes are formed.Before executing these byte codes,they are preprocessed by a ByteCodeVerifier.The ByteCode Verfier examines the byte codes,to check  is there any statement that violates the semantics of java.If any such statements are found then this verify error will be thrown.

Here i have presented you three reasons upon which this error may occur as follows.

Reason 1:

"Whenever we try to extend a class which is declared as final then this error will be thrown". Have a look at the following program to understand this reason.

Program:

class B extends A
{
public static void main(String args[])
{
System.out.println("my super class name:-"+myname);
}

 public class A
{
static String myname="A";
}

As you see if you compile this two programs and execute it,it must have to work fine without showing any error. Now I am going to change the class A  as follows and compile it alone. Note that here i have recompiled the "class A" alone.Now if i execute the class B (class that contains main() method) then an error message like below will be thrown at run-time.


final public class A
{
static String myname="A";
}


 Exception in thread "main" java.lang.VerifyError: Cannot inherit from final clas
s
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(Unknown Source)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$000(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: B.  Program will exit.

Reason 2:


 "Consider a class that extends an other class before and if it no longer extends that class now,then this error may be thrown at run-time."Look at the following program to see how the second reason causes this error.


 Program:

class C extends B
{
public static void main(String args[])
{
B b=new B();
display(b);
}
public static void display(A a)
{
System.out.println(a.supername);
}
}


class B extends A
{
String subname="B";
}


 public class A
{
String supername="A";
}

 The above program will also works fine,but if i change the class B to no longer extend the class A then this error may get thrown.Now if i change the class B as follows,and "recompile it alone" ,then class C will have no idea about the changes made in class B thus causing this error.

class B
{
String subname="B";
}


Exception in thread "main" java.lang.VerifyError: (class: C, method: main signat
ure: ([Ljava/lang/String;)V) Incompatible argument to function
Could not find the main class: C.  Program will exit.

Reason 3:

"If we try to override a method which is declared as final then also this error will be thrown". Let us have classes A and B as follows:

Program:


class B extends A
{
public static void main(String args[])
{
A a=new A();
a.display();
}
void display()
{
super.display();
}
}

 public class A
{
String supername="A";
void display()
{
System.out.println("My name is"+supername);
}
}

In the class A if i change the method display() to be of final and "recompile it alone", then this verify error will be thrown if i execute the class B since  no other class can override this method.

output:

Exception in thread "main" java.lang.VerifyError: class B overrides final method
 display.()V
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClassCond(Unknown Source)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$000(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: B.  Program will exit.

Here you could have noticed that this Verify Error is thrown because " i have recompiled only the edited class" and not all the classes as a whole. So you may think that this error can be easily identified if you recompile all the classes as a whole by recompiling the class which contains the main() method.

Of course it is true but there are certain situations at which you cannot be able to identify this error at Compile time,which is mainly because of using two different version of  third-party libraries in your application. 

I have presented you an example to show you how this error occurs when we use third-party libraries in your system in my older post  java.lang.illegalaccesserror .


4 comments:

veera reddy sangham said...

Very nice....

Nivetha Ramadoss said...

can any1 plz tell me how to fix my error which comes due to reason 3..

pentayyagmr@blogspot.com said...

After compiling my code with java8 I am getting this issue and I am sure this is because different versions of incompatible third party jars. So how can I find the jar? and how to resolve this issue? every point is appreciated.


Caused by: java.lang.VerifyError: Expecting a stackmap frame at branch target 13
Exception Details:
Location:
com/vir/palla/helper/ContactHelperImpl.unbindResolverFactory(Lorg/apache/sling/api/resource/ResourceResolverFactory;)V @5: if_acmpne
Reason:
Expected stackmap frame at this location.
Bytecode:
0x0000000: 2ab4 008e 2ba6 0008 2a01 b500 8eb1

at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)

Naviya Nair said...

Great and Useful Article.

Online Java Course

Java Online Training

Java Course Online

J2EE training

online J2EE training

Best Recommended books for Spring framework

Java Interview Questions








Java Training Institutes in Chennai

Java Training in Chennai

J2EE Training in Chennai

java j2ee training institutes in chennai

Java Course in Chennai

Post a Comment

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