Home > Java > javaTutorial > body text

Why does Java.lang.VerifyError error occur in Java and how to solve this problem?

王林
Release: 2023-09-23 22:13:02
forward
1534 people have browsed it

Why does Java.lang.VerifyError error occur in Java and how to solve this problem?

Java.lang.VerifyError 是由 JVM(Java 虚拟机)引发的运行时错误。在运行时,验证过程会检查加载的有效性。 class 文件,如果 .class 文件违反任何约束,那么 JVM 将给出 Java.lang.VerifyError 错误。 Java 1.0 版本及更高版本中存在以下错误。

java.lang.LinkageError 扩展了 Java.lang.VerifyError,它指的是程序的链接过程出现问题时。

Java.lang.VerifyError在输出中的样子将会是:

Exception in thread "main" java.lang.VerifyError: (class: com/example/Way2Class, method: myMethod signature: ()V) Incompatible argument to function
   at com.example.MyClass.main(MyClass.java:10)
Copy after login

此输出包含导致错误的类的名称 (Way2class)、类路径 (com/example/Way2Class)、错误行 (10)、方法名称 (myMethod) 和错误消息(线程“main”java 中的异常) .lang.VerifyError)

出现此错误的原因有多种,如下所示:

  • 版本不匹配:需要注意的是,如果类文件是使用与执行代码所使用的Java编译器版本不同的版本进行编译的话,可能会导致VerifyError。

示例

public class VerifyErrorVersionExample {
   public static void main(String[] args) {
      System.out.println("Hello, program!");
   }
}
Copy after login

在该特定程序使用 Java 9 编译并在早期版本(例如 Java 8)上运行的情况下,输出将显示后续错误解释:

输出

Exception in thread "main" java.lang.VerifyError: Bad type on operand stack
Exception Details:
   Location:
      VerifyErrorVersionExample.main([Ljava/lang/String;)V @2: invokestatic
   Reason:
      Type 'java/lang/invoke/StringConcatFactory' (current frame, stack[0]) is not assignable to 'java/lang/invoke/MethodHandle'
   Current Frame:
      bci: @2
      flags: { }
      locals: { '[Ljava/lang/String;' }
      stack: { 'java/lang/invoke/StringConcatFactory' }
   Bytecode:
      0x0000000:  getstatic    #2   // Field java/lang/System.out:Ljava/io/PrintStream;
      0x0000003:  ldc          #3   // String Hello, world!
      0x0000005:  invokestatic #4   // Method java/lang/invoke/StringConcatFactory.makeConcatWithConstants:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/MethodHandle;
      0x000000a:  ldc          #5   // String 
      0x000000c:  iconst_1
      0x000000d:  anewarray   #6   // class java/lang/Object
      0x0000010:  dup
      0x0000011:  iconst_0
      0x0000012:  ldc          #7   // String !!!
      0x0000014:  aastore
      0x0000015:  invokevirtual #8   // Method java/lang/invoke/MethodHandle.invokeExact:([Ljava/lang/Object;)Ljava/lang/Object;
      0x000001a:  checkcast   #9   // class java/lang/String
      0x000001d:  invokevirtual #10  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
      0x0000020:  return  
Copy after login
  • 字节码损坏:如果对类文件的字节码进行修改或损坏,可能会出现错误。

示例

public class VerifyErrorBytecodeExample {
   public static void main(String[] args) {
      int a = 10;
      int b = 20;
      int c = a + b;
      System.out.println("The sum of " + a + " and " + b + " is " + c);
   }
}
Copy after login

输出

Exception in thread "main" java.lang.VerifyError: (class: VerifyErrorBytecodeExample, method: main signature: ([Ljava/lang/String;)V) Illegal target of jump or branch
      at java.lang.Class.getDeclaredMethods0(Native Method)
      at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
      at java.lang.Class.getDeclaredMethod(Class.java:2128)
      at java.io.ObjectStreamClass.getPrivateMethod(ObjectStreamClass.java:1743)
      at java.io.ObjectStreamClass.access$1700(ObjectStreamClass.java:72)
      at java.io.ObjectStreamClass$2.run(ObjectStreamClass.java:513)
      at java.io.ObjectStreamClass$2.run(ObjectStreamClass.java:492)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.io.ObjectStreamClass.<init>(ObjectStreamClass.java:492)
      at java.io.ObjectStreamClass.lookup(ObjectStreamClass.java:389)
      at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1134)
      at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
      at VerifyErrorBytecodeExample.main(VerifyErrorBytecodeExample.java:6)
Copy after login
  • 类文件不一致:如果类文件违反了Java虚拟机规范所强制的某些约束条件。

示例

public class VerifyErrorExample {
   public static void main(String[] args) {
      Animal animal = new Cat();
      animal.speak();
   }
}
interface Animal {
   void speak();
}
class Dog implements Animal {
   public void speak() {
      System.out.println("Woof!");
   }
}
class Cat extends Dog {
   public void speak() {
      System.out.println("Meow!");
   }
}
Copy after login

这个程序定义了一个‘Animal’接口和两个实现它的具体类:Dog和Cat。Cat继承了Dog并重写了它的speak方法。在主方法中,我们创建了一个Cat实例并调用了它的speak方法。

假设我们选择从当前类路径中取出 Dog 类,然后重新编译并运行我们的程序,将会显示 java.lang。验证错误。此错误将显示如下错误消息:

输出

Exception in thread "main" java.lang.VerifyError: Bad type on operand stack
Exception Details:
   Location:
      VerifyErrorExample.main([Ljava/lang/String;)V @2: invokevirtual
   Reason:
      Type 'Cat' (current frame, stack[0]) is not assignable to 'Dog'
   Current Frame:
      bci: @2
      flags: { }
      locals: { '[Ljava/lang/String;' }
      stack: { 'Cat' }
   Bytecode:
      0000000:  new           #2                  // class Cat
      0000003:  dup
      0000004:  invokespecial #3                  // Method Cat."":()V
      0000007:  astore_1
      0000008:  aload_1
      0000009:  invokevirtual #4                  // Method Animal.speak:()V
      000000c:  return
Copy after login

结论

总之,遇到java.lang.VerifyError错误可能是由于各种原因引起的,包括字节码波动、安全计算实践中的限制,以及代码库中包含的Java组件的不一致互相依赖版本等等。在运行时,确保所有使用的库与您的系统和其他依赖库兼容是避免此问题的一种非常重要的方式。遵守这些要求将确保遵守Java的字节码规定,避免出现问题的编码实践,例如不符合安全协议或提交具有类文件不一致性的代码,这可能导致后续出现错误。每当java.lang.VerifyError出现时,必须仔细检查相关的错误消息以及您可以获取的堆栈跟踪,以确定正在发生的确切问题并采取必要的纠正措施。

The above is the detailed content of Why does Java.lang.VerifyError error occur in Java and how to solve this problem?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template