NoClassDefFoundError and classnotfoundexception exceptions

Time:2022-5-27

1. Introduction

Classnotfoundexception and NoClassDefFoundError occur when the JVM cannot find the class requested by the classpath.

2.ClassNotFoundException

Classnotfoundexception is a checked exception that occurs when an application attempts to load a class with its fully qualified name and cannot find it on the classpath.

Mainly occurs when trying to use class forName()、ClassLoader. loadClass()、ClassLoader. Findsystemclass() failed to load class.

For example: load a class that does not exist

Class.forName("acb.s.sad")

3.NoClassDefFoundError

In case of NoClassDefFoundError (Java project fhadmin. CN), the system cannot operate normally and cannot be recovered. When the JVM tries the following:

Instantiate a class with new

Loading classes using method calls

When the compiler can successfully compile the class, but the Java runtime cannot find the class file, a modification error will occur. It usually occurs when an exception occurs when executing a static block or initializing a static field of a class, so the class initialization fails

4. Solutions

The main reason is that the runtime class file (in the classpath) is not available. resolvent:

Ensure that the class or jar containing the class can be used in the classpath. If not, you need to add it manually.

If it is available on the classpath, it is possible that the path is overwritten. At this time, you need to find the exact path of the class.

5. Summary

Classnotfoundexception and NoClassDefFoundError are both because the Java runtime cannot find the relevant class.

Classnotfoundexception is an exception thrown when the Java runtime loads a class, and the name is provided at runtime.

NoClassDefFoundError means that this class exists at compile time, but cannot be found on the path by Java runtime.

This work adoptsCC agreement, reprint must indicate the author and the link to this article

Recommended Today

Five landing pains behind serverless agitation

Source|Alibaba cloud native official account ServerlessFrom birthIs given the mission of changing cloud computinghoweverSomething that often looks more powerful The more difficulties you encounter when landing ServerlessNo exception** Pain point 1: Manufacturer lock Whether individual users or enterprise customers Everyone doesn’t want to be locked by the manufacturerInstead, they let themselves choose services and products […]