Why java.lang.VerifyError Occurs in Java and How to Solve this?
Last Updated :
30 May, 2022
The Java Virtual Machine (JVM) distrusts all loaded bytecode as a core tenet of the Java Security Model. During runtime, the JVM will load .class files and attempt to link them together to form an executable — but the validity of these loaded .class files is unknown. To ensure that the loaded .class files do not pose a threat to the final executable, a verification process is done on the .class files by the JVM.
Additionally, the JVM ensures that binaries are well-formed. For example, the JVM will verify classes do not subtype final classes. In many cases, verification fails on valid, non-malicious bytecode because a newer version of Java has a stricter verification process than older versions. For example, JDK 13 may have added a verification step that was not enforced in JDK 7. Thus, if we run an application with JVM 13 and include dependencies compiled with an older version of the Java Compiler (javac), the JVM may consider the outdated dependencies to be invalid.
Thus, when linking older .class files with a newer JVM, the JVM may throw a java.lang.VerifyError.
The VerifyError exists since the 1.0 version of Java.
The Structure of VerifyError:
Constructors
VerifyError()
This constructor creates an instance of the VerifyError class, setting null as its message.
VerifyError(String s)
This constructor creates an instance of the VerifyError class, using the specified string as message. Here the class which threw the error is indicated through string argument.
The three most common reasons upon which this error may occur as follows:
Reason 1: “This error will be thrown whenever a class which is declared as final is extended.”
Program:
Java
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 these two programs and execute it, it must have to work fine without showing any error. Now after changing the class A as follows and compile it alone.
final public class A
{
static String myname="A";
}
Note that here we have recompiled the “class A” alone. Now if we execute the class B (class that contains main() method) then an error message like below will be thrown at run-time.
Exception in thread "main" java.lang.VerifyError: Cannot inherit from final class
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.
This error was caused because that we changed the definition of class TestClassA, but class TestClassB was compiled using an older version of the class TestClassA.
Reason 2: “Consider a class that extends another class before and if it no longer extends that class now, then this error may be thrown at run-time.”
Program:
Java
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 work fine, but if class B is changed to no longer extend class A then error may get thrown. Now if we 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 signature: ([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:
Java
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 class A if the method display() is changed to be of final and “recompile it alone”, then this verify error will be thrown if class B is executed 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 verifies Error is thrown because we 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 in which you cannot be able to identify this error at Compile time, which is mainly because of using two different versions of third-party libraries in your application.
How to deal with the VerifyError?
In order to avoid the VerifyError, you must compile all your classes using the same version of Java. Also, once a change is done to a class, then make sure that you re-compile your project from scratch. Finally, if your application makes use of external libraries, verify that you use the appropriate version of every library and of course, consult the corresponding javadocs, in order to be sure that everything is correct.
Whenever possible, use the latest versions of dependencies rather than disabling verification.
Similar Reads
How to Fix java.lang.UnsupportedClassVersionError in Java?
The UnsupportedClassVersionError is a sub-class of the LinkageError and thrown by the Java Virtual Machine (JVM). When a class file is read and when major and minor version numbers are not supported, this error is thrown, and especially during the linking phase, this error is thrown Situations when
3 min read
How to Solve java.lang.NoSuchMethodError in Java?
A java.lang.NoSuchMethodError as the name suggests, is a runtime error in Java which occurs when a method is called that exists at compile-time, but does not exist at runtime. The java.lang.NoSuchMethodError can occur in case application code is partially compiled, or in case an external dependency
3 min read
How to Solve java.lang.ClassNotFoundException in Java?
In Java, java.lang.ClassNotFoundException is a checked exception and occurs when the Java Virtual Machine (JVM) tries to load a particular class and the specified class cannot be found in the classpath. ClassNotFoundException should be handled with a try-catch block or using the throw keyword. In ol
4 min read
How to Resolve Java.lang.ExceptionInInitializerError In Java?
An unexpected, unwanted event that disturbed the normal flow of a program is called an Exception. There are mainly two types of exception in Java: 1. Checked Exception 2. Unchecked Exception ExceptionInInitializerError is the child class of the Error class and hence it is an unchecked exception. Thi
3 min read
How to Fix java.util.NoSuchElementException in Java?
An unexpected, unwanted event that disturbed the normal flow of a program is called Exception. Most of the time exceptions are caused by our program and these are recoverable. Suppose if our program requirement is to read data from the remote file locating in the U.S.A. At runtime, if a remote file
4 min read
How to check that the element is clickable or not in Java Selenium WebDriver?
Ensuring that an element is clickable in your Selenium WebDriver tests is crucial for validating the functionality and user experience of your web applications. In Java Selenium WebDriver, checking if an element is clickable before interacting with it can help avoid errors and ensure that your test
3 min read
How to Resolve The Cannot Find Symbol Error in Java?
The Cannot Find Symbol Error in Java error occurs when the Java compiler cannot find a symbol that you are trying to reference in your code. In this article, we will explore how to resolve the "Cannot Find Symbol" error in Java. The "Cannot Find Symbol" error occurs when you are trying to reference
5 min read
How to Update the Java Version in Windows?
Java is a high-level, robust, object-oriented, and secure programming language that was developed in the year 1995 by Sun Microsystems (which is now part of Oracle group), and James Gosling is known as the father of Java. Â It is a general-purpose programming language intended to let programmers writ
3 min read
java.net.PasswordAuthentication Class in Java
PasswordAuthentication class is provided by package java.net for implementing networking applications, and it is used in those cases when it is required to hold the data that will be used by the Authenticator. It holds the username and password. Syntax of its constructors : PasswordAuthentication(St
2 min read
How to Handle Alert in Selenium using Java?
Imagine filling out a form online and accidentally missing some information. You only know if you made a mistake if the website tells you somehow, like with a pop-up message. This article explains what those pop-up messages are called in Selenium (alerts) and how to deal with them in your automated
6 min read