How to solve Java.lang.NoClassDefFoundError__Java

Source: Internet
Author: User

Original:

http://vipcowrie.iteye.com/blog/1561291

http://vipcowrie.iteye.com/blog/1562251

In Java development, throwing java.lang.NoClassDefFoundError in the main thread is a very common and difficult problem to solve. The complexity of solving this problem depends largely on your software size and middleware deployment, especially considering the large number of classloader that occur in your application.

This article will look at this problem from a relatively high point of view, mainly introduces the Java ClassLoader mechanism.

So, what is Java.lang.NoClassDefFoundError?

Let's take a quick look at the problem, the runtime exception is thrown by the JVM, which is thrown when the JVM finds a classloader trying to load a class that is not found in the current ClassLoader tree.

Obviously, this problem is a run-time problem, and everything works during the compile period.

So, the solution is very simple, that is, put the jar bag under the classpath not on the line.

OK, not here yet, this problem is not so easy to solve, in the runtime of the program Classpath to add the missing jar package is only a workaround. The key is that we have to master the root cause of this anomaly, and we can status quo this problem later. This is the original intention of my writing this article.

Now, remember that this problem is not necessarily due to the lack of class definition in classpath.

Java ClassLoader Overview

Before we get into the deep analysis, we have to master the fundamentals of Java ClassLoader. Class loader is a Java object that is responsible for all class of load, and is responsible for locating, loading, and generating the basic definition information of a class. ClassLoader itself uses the principal-agent mechanism to query class, each ClassLoader instance has a parent classloader, so when an application ClassLoader to load Class A, The first thing that happens is that ClassLoader delegates its parent ClassLoader to load class A, and after a string of chained lookups, the final task falls on the JVM's system startup ClassLoader.

There's going to be a problem. When you expect your application ClassLoader to load Class A, but when class A is queried and loaded by any of its parent classloader, then java.lang.NoClassDefFoundError can occur. When all of the parent ClassLoader cannot find Class A, they are tried to load by the application's own ClassLoader.


This article is for Java beginners, and recommends that you compile and run the example program yourself.

This paper contains the reason analysis and example program of Noclassdeffounderror, and gives the proposed processing strategy.

Noclassdeffounderror problem reason 1: Missing jar Package

The first and most common cause is the classpath configuration issue. Example program:

This example program attempts to create a new Callerclassa instance and then executes one of his methods, which references the class Referencingclassa, and this example demonstrates the noclassdeffounderror caused by the classpath problem, This example also prints the current situation of ClassLoader chain for further analysis. This print information will also be helpful for you to analyze these problems in the future:

Program
Java Code    public class noclassdeffounderrorsimulator {            public static void main (String[] args)  {                       system.out                                    . println ("Java.lang.noclassdeffounderror simulator");                       // Print current  classloader context                       system.out.println ("\ncurrent classloader chain: "        &NBSp;                             + classloaderutil.getcurrentclassloaderdetail ());                       // 1. Create a new instance of CallerClassA                       Callerclassa caller = new callerclassa ();                       // 2. execute method  of the caller                       caller.dosomething ();             &nBsp;         system.out.println ("done!");               }  }  


Java Code    public class callerclassa {            private final static string clazz = callerclassa.class.getname ();               static {                       System.out.println ("classloading of "  + CLAZZ +  " in progress ...")                                     +  Classloaderutil.getcurrentclassloaderdetail ());               }              public callerclassa ( )  {                       System.out.println ("creating a new instance of "                                      + callerclassa.class.getname ()  +  " ... ");              }               public void dosomething ()  {                      // create  a new instance of ReferencingClassA                       ReferencingClassA  Referencingclass = new rEferencingclassa ();              }  }   

Java code    public class referencingclassa {               private final static String CLAZZ =  ReferencingClassA.class.getName ();               static {                       system.out.println ("classloading of "  + CLAZZ +  "  In progress ... "                                     + classloaderutil.getcurrentclassloaderdetail ());               }              public  Referencingclassa ()  {                       system.out.println ("creating a new instance of "                                      + referencingclassa.class.getname ()  +  "...");                                        maps.newhashmap ();               }              public void  DoSomething ()  {  

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.