A,

There will always be problems in development that require a lot of hammering, even if it takes half a day to solve. And ready to say next Java. Lang. NoClassDefFoundError and ClassNotFoundException, is to let a person teeth problem # 2. At first glance, both problems seem to indicate that a class is not found, and while some of the causes of both problems are the same, in fact, the two errors are completely different. Too often we spend time trying to solve the problem in some other way, without really understanding the cause of the error. There are many reasons for these two problems, but most of us often encounter one or two of them. Therefore, in order to help people in need, I summarize several ways to solve these two problems according to my previous development experience, and briefly explain the differences between them.

Second, solutions

Let’s start with solutions. After all, most people are more concerned with solving problems. If I was tuning the compiler, I would take screenshots from listing both Idea Intellij and Eclipse. Ps: I often use idea, of course, Eclipse will also use, but relatively speaking, less just. The following solutions are the same without being specified.

Solution 1: Check whether the corresponding JAR package or dependency is imported successfully.

The idea of:

In Eclipse: right-click the project and choose Build Path->Configure Build Path to enter the project configuration

Check whether the libs in the WEN-INF directory contains any imported packages.

The idea of:

Eclipse:

Solution 3: Paste the package into the WEB-INF lib, but it still doesn’t work, then you need to check whether to add it to the library.

The idea of:

Eclipse:

Solution 4: The CLASSPATH environment variable is incorrectly configured.

In the system environment variables column midpoint – >; The new classpath. Variable name :classpath Variable value:.; %JAVA_HOME%/lib/; %JAVA_HOME%/jre/lib/ (note that CLASSPATH starts with a ‘. Is the current directory, so when we run javaAClass, the system will first look for AClass files in the current directory.

NoClassDefFoundError can also be caused by a class static initialization module error.

When your class performs some static module initialization operations, if the initialization module raises an exception, which other classes that depend on this class will raise a NoClassDefFoundError error. Log if you look at the program, you’ll find some Java. Lang. ExceptionInInitializerError error log, ExceptionInInitializerError mistake can lead to Java. Lang. NoClassDefFoundError: Could not initialize class.

Solution 6: You may run your program using the JAR command, but the class is not defined in the CLASspath property in the MANIFEST file of the JAR file.

The idea of:

Solution 7: Permission problems on Jar files may also cause NoClassDefFoundError.

If your application is running on a multi-user operating system such as Linux, you need to assign permissions to the application’s related resource files, such as Jar files, class library files, and configuration files, to the user group to which the application belongs. If you use Jar packages shared by multiple users and different applications, permissions can easily occur. Like other user privilege application belongs to the jar package your program does not have permission to access, can lead to Java. Lang. NoClassDefFoundError error.

Third, summary

We are often Java. Lang. ClassNotFoundException and Java lang. NoClassDefFoundError this confused two mistakes is not clear, although they are related to the Java classpath, but they are completely different. NoClassDefFoundError occurred in the JVM in dynamic runtime, according to you provide the name of the class, find the corresponding class in the classpath to load, but when it can’t find the class, a Java has taken place. The lang. NoClassDefFoundError error, ClassNotFoundException is an error that occurs when the corresponding class is not found in the classpath at compile time. ClassNotFoundException is easier to fix than NoClassDefFoundError because we know the error occurred at compile time and it is entirely due to an environmental problem. If you are working in a J2EE environment and you get a NoClassDefFoundError exception, and the wrong class does exist, this means that the class may not be visible to the class loader. To put it bluntly, Java. Lang. In less than a ClassNotFoundException can’t see, Java. Lang. ClassNotFoundException see it won’t.