Java.lang.NoClassDefFoundError exception

Source: Internet
Author: User
Tags error handling visibility file permissions
In daily Java development, we often encounter java.lang.NoClassDefFoundError such mistakes, it takes a lot of time to find the cause of the error, specifically which class is missing. The class is still in, why can't find it. And it's easy to confuse java.lang.NoClassDefFoundError with java.lang.ClassNotfoundException, in fact these two errors are completely different. We often take the time to try some other way to solve the problem without really understanding the cause of the mistake. This article is to uncover some secrets of noclassdeffounderror by solving the experience of noclassdeffounderror error handling. Noclassdeffounderror's mistakes are not unresolved or difficult to solve, but the form of such errors can easily confuse other Java developers. Let's analyze why Noclassdeffounderror and how to solve this error.


Noclassdeffounderror the reason for the error occurred


The Noclassdeffounderror error occurs because the Java Virtual machine can find the appropriate class at compile time and cannot find the appropriate class error at run time. For example, at runtime, when we want to invoke a method of a class or access a static member of the class, we find that the class is not available, and the Java virtual machine throws a noclassdeffounderror error. Unlike classnotfoundexception, this error occurs only when the corresponding class is not successful at run time, not at compile time. Many Java developers can easily confuse these two errors here.


The simple summary is that the noclassdeffounderror occurs when the corresponding class is available at compile time, and the runtime is in the Java classpath path, and the corresponding class is not the error caused. When a noclassdeffounderror error occurs, you can see the following error log:


Exception in thread "main" Java.lang.NoClassDefFoundError
The wrong message clearly indicates that the main thread could not find the specified class, and this main thread might be the main or other child thread. If the main thread has an error, the program crashes or stops, and if it is a child thread, the child thread stops and the other threads continue to run.


The difference between Noclassdeffounderror and classnotfoundexception


We are often confused by the mistakes of Java.lang.ClassNotFoundException and Java.lang.NoClassDefFoundError, although they are all related to Java classpath, but they are completely different. Noclassdeffounderror occurs when the JVM is dynamically running, and the corresponding class is found in the classpath according to the class name you provide, but when it cannot find the class, A java.lang.NoClassDefFoundError error occurs, and classnotfoundexception is not found in the classpath when compiling the corresponding class. ClassNotFoundException is easier to solve than noclassdeffounderror because we know that the error occurs at compile time and it is entirely due to environmental problems. And if you work under the Java EE Environment and get noclassdeffounderror exceptions, and the corresponding error class is real, this class may be invisible to the ClassLoader.


How to solve Noclassdeffounderror error


According to the previous article, it is obvious that the Noclassdeffounderror error is because the class loader cannot find the classes to load under Classpath at runtime, so we need to load the corresponding class into the classpath. Or to check why the class is not available in classpath, this can occur because of the following:


The corresponding class is not available in the Java classpath
You may run your program with the jar command, but the class is not defined in the Classpath attribute in the jar file's manifest file.
The startup script for the program may overwrite the original CLASSPATH environment variable
Because Noclassdeffounderror is a subclass of Java.lang.LinkageError, it may be caused by a program-dependent native class library that is not available
Check the log file for errors such as Java.lang.ExceptionInInitializerError, Noclassdeffounderror may have been caused by static initialization failure
If you work in the Java EE environment, there are several different class loaders that can also cause noclassdeffounderror
Let's look at some examples of how we can solve the noclassdeffounderror when it happens.


Noclassdeffounderror resolution Example


A java.lang.NoClassDefFoundError error occurs when a jar file is missing, or if the jar file is not added to the classpath, or if the name of the jar is changed.
This is difficult to know exactly when a class is not in Classpath, but if you print out System.getproperty ("Java.classpath") in your program, you get the classpath that the program actually runs
The runtime clearly specifies that you think the program can run normally-classpath parameters, if the program can be increased after the normal operation, indicating that the original program of the Classpath was covered by others.
Noclassdeffounderror may also be caused by static initialization module errors for classes, when your class performs some static initialization module operations, and if the initialization module throws an exception, which other classes that rely on the class will throw a noclassdeffounderror error. If you view the program log, Some java.lang.ExceptionInInitializerError error logs will be found, and exceptionininitializererror errors can lead to Java.lang.NoClassDefFoundError : Could not initialize class, as shown in the following code example:
/**
* Java program to demonstrate how failure of static initialization subsequently cause
* Java.lang.NoClassDefFoundError in Java.
* @author Javin Paul
*/
public class Noclassdeffounderrorduetostaticinitfailure {


public static void Main (String args[]) {


list<user> users = new arraylist<user> (2);


for (int i=0; i<2; i++) {
try{
Users.add (New User (string.valueof (i))); would throw Noclassdeffounderror
}catch (Throwable t) {
T.printstacktrace ();
}
}
}
}


Class user{
private static String user_id = GetUserID ();


Public User (String ID) {
This. user_id = ID;
}
private static String GetUserID () {
throw new RuntimeException ("UserId not Found");
}
}


Output
Java.lang.ExceptionInInitializerError
At testing. Noclassdeffounderrorduetostaticinitfailure.main (noclassdeffounderrorduetostaticinitfailure.java:23)
caused by:java.lang.RuntimeException:UserId not found
At testing. User.getuserid (noclassdeffounderrorduetostaticinitfailure.java:41)
At testing. User.<clinit> (noclassdeffounderrorduetostaticinitfailure.java:35)
... 1 more
Java.lang.NoClassDefFoundError:Could not initialize class testing. User
At testing. Noclassdeffounderrorduetostaticinitfailure.main (noclassdeffounderrorduetostaticinitfailure.java:23)




Read More:http://javarevisited.blogspot.com/2011/06/noclassdeffounderror-exception-in.html#ixzz3dqtbvhdy
Because Noclassdeffounderror is a subclass of Linkageerror, and linkageerror errors occur when dependent on other classes, if your program relies on the native class library and the DLL you need not exist, It is possible that java.lang.NoClassDefFoundError may appear. This error can also throw exceptions such as Java.lang.UnsatisfiedLinkError:no DLL in Java.library.path Exception java. The solution is to put the dependent class libraries and DLLs together with your Jar pack.
If you use ant build scripts to generate jar files and manifest files, make sure that the ant script gets the correct Classpath value written to the MANIFEST.MF file
Permissions issues with jar files can also lead to Noclassdeffounderror, if your program is running on a multi-user operating system such as Linux, you need to apply the relevant resource files, such as jar files, class library files, and configuration file permissions to the user group of the program. If you use a jar package shared by multiple users in different programs, you can easily get permission issues. For example, other users to apply the permissions of the jar package your program does not have access to permissions, can cause Java.lang.NoClassDefFoundError errors.
Programs that are based on XML configuration can also cause noclassdeffounderror errors. For example, most Java frameworks like spring,struts use XML configuration to get the corresponding bean information, if you enter the wrong name, the program may load other wrong classes and cause Noclassdeffounderror exception. We are using the Spring MVC Framework or the Apache Struts framework, where the exception in thread "main" java.lang.NoClassDefFoundError is often present when you deploy a war file or an ear file.
In the environment with multiple classloader, it is easy to noclassdeffounderror errors. Because Java-EE does not specify the standard ClassLoader, the class loader that is used relies on different components like Tomcat, Weblogic,websphere loaded Java, or a Ejb-jar package. The relevant knowledge about ClassLoader can refer to the working principle of this article ClassLoader.


In summary, the ClassLoader is based on three mechanisms: delegate, visibility, and singleness, which means that a request to load a class is given to the parent class loader, which is then loaded if the parent loader cannot find or load the class. The principle of visibility is that the classloader of a subclass can see all classes loaded by the parent ClassLoader, and the parent class loader does not see the classes loaded by the subclass loader. The principle of singleness is to load only one class at a time, which is determined by the delegate mechanism to ensure that the subclass loader does not load classes loaded by the parent ClassLoader again. Now assume that a user class exists in both the war file and the Ejb-jar file and is loaded by the war ClassLoader, while the war ClassLoader is the child classloader that loads the Ejb-jar ClassLoader. When code in Ejb-jar refers to the user class, the ClassLoader that loads Ejb-jar All classes cannot find this class, because the class has been loaded by the Ejb-jar ClassLoader's child loader war ClassLoader.


This results in a Noclassdeffounderror exception to the user class, and if the user class exists in two jar packages, a classcastexception exception occurs if you use the Equals method to compare objects of two classes. Because two different class loaders load classes cannot be compared.


Sometimes it appears that exception in thread "main" java.lang.noclassdeffounderror:com/sun/tools/javac/main such a mistake, this error indicates your classpath, PATH or java_home is not installed properly or the JDK is not installed correctly. The solution to this problem is to reinstall your JDK.


Java can also cause noclassdeffounderror when performing linking operations. For example, in the previous script, if after compiling, we delete the user's compile file, then run the program, this time you will get noclassdeffounderror directly, and the wrong message only prints out the name of the user class.
Java.lang.noclassdeffounderror:testing/user
At testing. Noclassdeffounderrorduetostaticinitfailure.main (noclassdeffounderrorduetostaticinitfailure.java:23)
Now we know how to face the noclassdeffounderror anomaly and solve it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.