About Pierre Hugues Charbonneau

Pierre-Hugues Charbonneau (nickname P-H) is working for CGI Inc. Canada for the last 10 years as a senior IT consultant. His primary area of expertise is Java EE, middleware & JVM technologies. He is a specialist in production system troubleshooting, root cause analysis, middleware, JVM tuning, scalability and capacity improvement; including internal processes improvement for IT support teams. P-H is the principal author at Java EE Support Patterns.

java.lang.NoClassDefFoundError: How to resolve – Part 1

Exception in thread ‘main’ java.lang.NoClassDefFoundError is one of the common and difficult problems that you can face when developing Java EE enterprise or standalone Java applications. The complexity of the root cause analysis and resolution process mainly depend of the size of your Java EE middleware environment; especially given the high number of ClassLoaders present across the various Java EE applications.

What I’m proposing to you is a series of articles which will provide you with a step by step approach on how to troubleshoot and resolve such problem. I will also share the most common Java NoClassDefFoundError problem patterns I have observed over the last 10 years. Sample Java programs will also be available in order to simplify your learning process. I also encourage you to post comments, share your problem case and ask me any question on this subject.
The part 1 of the series will focus on a high level overview of this Java runtime error along with a Java ClassLoader overview.

java.lang.NoClassDefFoundError – what is it?

Now let’s begin what a simple overview of this problem. This runtime error is thrown by the JVM when there is an attempt by a ClassLoader to load the definition of a Class (Class referenced in your application code etc.) and when such Class definition could not be found within the current ClassLoader tree.

Basically, this means that such Class definition was found at compiled time but is not found at runtime.

Simple enough, what about adding the missing Class to the classpath?

Well not so fast, this type of problem is not that simple to fix. Adding the missing Class / JAR to your runtime application classpath / ClassLoader is just one of the many possible solutions. The key is to perform proper root cause analysis first. This is exactly why I’m creating this whole series.
For now, just keep in mind that this error does not necessarily mean that you are missing this Class definition from your “expected” classpath or ClassLoder so please do not assume anything at this point.

Java ClassLoader overview

Before going any further, it is very important that you have a high level of understanding of the Java ClassLoader principles. Quite often individuals debugging NoClassDefFoundError problems are struggling because they are lacking proper knowledge and understanding of Java ClassLoader principles; preventing them to pinpoint the root cause.

A class loader is a Java object responsible for loading classes. Basically a class loader attempts to locate or generate data that constitutes a definition for the class. One of the key points to understand is that Java class loaders by default use a delegation model to search for classes. Each instance of ClassLoader has an associated parent class loader. So let’s say that your application class loader needs to load class A. The first thing that it will attempt to do is to delegate the search for Class A to its parent class loader before attempting to find the Class A itself. You can end up with a large class loader chain with many parent class loaders up to the JVM system classpath bootstrap class loader.

What is the problem? Well if Class A is found from a particular parent class loader then it will be loaded by such parent which open the doors for NoClassDefFoundError if you are expecting Class A to be loaded by your application (child) class loader. For example, third part JAR file dependencies could only be present to your application child class loader.

Now let’s visualize this whole process in the context of a Java EE enterprise environment so you can better understand.

As you can see, any code loaded by the child class loader (Web application) will first delegate to the parent class loader (Java EE App). Such parent class loader will then delegate to the JVM system class path class loader. If no such class is found from any parent class loader then the Class will be loaded by the child class loader (assuming that the class was found).

Please note that Java EE containers such as Oracle Weblogic have mechanisms to override this default class loader delegation behavior. I will get back to this in the future articles.

Please feel free to post any comment or question of what you learned so far. The part 2 will follow shortly.

Reference: java.lang.NoClassDefFoundError: How to resolve – Part 1 from our JCG partner Pierre-Hugues Charbonneau at the Java EE Support Patterns & Java Tutorial blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

9 Responses to "java.lang.NoClassDefFoundError: How to resolve – Part 1"

  1. Rajasekharan R says:

    Hi

    Could you please elaborate on “which open the doors for NoClassDefFoundError if you are expecting Class A to be loaded by your application (child) class loader”

    thanks

    • Pierre-Hugues Charbonneau says:

      Hi Rajasekharan,A simple example is like this. Let’s say you add a third part API to your WebApp WEB-INF/lib folder (child classloader). This API could contains let’s say 5 JAR files. If one such JAR file (that contains Class A) is found at the parent classloader or System class path then Class A is loaded by such parent class loader. You may not realize this unless you perform proper parent classloader analysis.The problem is triggered when Class A attempts to load Java classes only found in your WebApp parent classloader e.g. when Class A is attempting to invoke other referencing classes not found at the parent classloader itself; remember, the delegation model is from bottom-up, not the other way around. Since the referencing code is ONLY present at your child class loader, then NoClassDefFoundError is thrown since referencing code within Class A is not found from either current or parent class loader. I will provide some Java sample programs in future articles so you can better understand these problems.Thanks.P-H

  2. Shreyash says:

    Hi,
    I have to write a program which creates, compiles and executes a java file on the fly. But the class loader is not able to find the class even if I load the class explicitely. Please suggest how can I load the class at runtime?

  3. Arun Singh says:

    DONT WORRY! ACTUALLY IT’S A GENERIC JAVA COMPILATION ERROR.
    YOU CAN SOLVE THIS PROBLEM BY–
    -RENAME THE ‘CLASS NAME’ THAT YOU HAVE PROVIDED INSIDE THE JAVA FILE CODE USING NOTEPAD…
    -REMOVE SOME LETTER FROM THE CLASS NAME, THEN SAVE IT AGAIN…
    -NOW COMPILE IT AGAIN
    -THE COMPILATION AND RUN PROCESS WILL DONE PROPERLY!
    NOW YOU CAN CHANGE THE CLASS FILE NAME AGAIN THAT U WANT….
    ##100% works this method##

  4. Abid says:

    Well 90% of the times I faced this error was due to missing jar on class path. only couple of times because of other reasons.

  5. Mohit says:

    NoClassDefFoundError comes when the specified class was present during compilation but are missing at runtime. I had this problem as some class files were missing from classpath, when I added corresponding jar file then all errors were gone.

    Below link may be useful

    http://newtechnobuzzz.blogspot.in/2014/07/what-is-javalangclassnotfoundexception.html

  6. Chandan says:

    Hello Pierre,
    Interesting article! Waiting eagerly for the next part..

Leave a Reply


− 1 = three



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close