6.08.2012

Exception in thread "main" java.lang.NoClassDefFoundErroris one of the most 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 will depend of the size of your Java EE middleware environment, especially given the high number of class loaders 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.

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.

45
comments:

Hi P-H, yet again great post. I think key to work with this error is good knowledge of How Classpath works in Java and Classloaders. I have seen many programmer not able to differentiate between NoClassDefFoundError and ClassNotFoundException which adds into nightmare. I can say Core Java is little bit different than J2EE where Classloader comes lot more in picture.

Hello sir, I am developing an application in which i am using android plugins developed in Eclipse IDE. I have created two classes, first is the main activity class and second is a service class.But when i export and extract the JAR file; it only shows the main activity class(and not the service class) and when i use this plugin in my application it shows the following error: =>java.lang.NoClassDefFoundError

Note:1)for plugin development i am using Eclipse. 2)i have entered both classes in android manifest file. 3)both classes are in the same package.

It looks like that your export process is not properly packaging your service class. Since at runtime your main activity class is looking for your service class, java.lang.NoClassDefFoundError will be thrown since the runtime reference of this class is not found.

Can you please open your exported JAR file and let me know what you see? You can also try to manually create your JAR file e.g. simply grab your missing service class from the generated Eclipse output folder and add it back to the exported JAR file.

hai P-H i was just a beginner core java, at the beginning our teacher asked us to put path environment variable using system settings and i put "E:\Program Files\Java\jdk1.6.0\bin;" this path value, instead of setting path every time, @ that time i dint face any problem i can execute my program easily...

now the thing is i am getting "java.lang.noclassdeffounderror" main thread exception! i cant even able 2 execute previously programs!! can u plz helm me out

I have checked out a project from our CVS repository and trying to run.But it shows the following error:Exception in thread "main" java.lang.NoClassDefFoundError: com/sankya/itech/dao1/CustomersDAOCaused by: java.lang.ClassNotFoundException: com.sankya.itech.dao1.CustomersDAO at java.net.URLClassLoader$1.run(URLClassLoader.java:217) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:205) at java.lang.ClassLoader.loadClass(ClassLoader.java:323) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294) at java.lang.ClassLoader.loadClass(ClassLoader.java:268) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:336)

Exception in thread "main" java.lang.NoClassDefFoundError: com/beans/MyBean at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631) at java.lang.ClassLoader.defineClass(ClassLoader.java:615) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141) at java.net.URLClassLoader.defineClass(URLClassLoader.java:283) at java.net.URLClassLoader.access$000(URLClassLoader.java:58) at java.net.URLClassLoader$1.run(URLClassLoader.java:197) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:169) at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:100) at org.hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:70) at org.hibernate.mapping.SimpleValue.setTypeUsingReflection(SimpleValue.java:276) at org.hibernate.cfg.HbmBinder.bindSimpleId(HbmBinder.java:401) at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(HbmBinder.java:334) at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:273) at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:144) at org.hibernate.cfg.Configuration.add(Configuration.java:669) at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:504) at org.hibernate.cfg.Configuration.addResource(Configuration.java:566) at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1587) at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534) at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508) at org.hibernate.cfg.Configuration.configure(Configuration.java:1428) at com.facadeimplementation.hibernate.util.HibernateFactory.configureSessionFactory(HibernateFactory.java:83) at com.facadeimplementation.model.dao.MyDao.(MyDao.java:23) at com.facadeimplementation.model.dao.MappingDao.(MappingDao.java:23) at com.ui.MainForm.(MainForm.java:45) at com.ui.MainForm$1.run(MainForm.java:77) at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332) at com.ui.MainForm.main(MainForm.java:74)Caused by: java.lang.ClassNotFoundException: com.beans.MyBean at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) ... 37 more

I'm able to run this project successfully in Eclipse.

MyBean.java is contained in a Jar. I have so many POJOs used in the project so it's not possible to use them directly instead of using them from within a jar.

I found a typo in your start-up command ":DynamicJasper-2.0.8:". It is missing .jar and could prevent the rest of the classpath. Also, please move mybeans.jar at the beginning to we can ensure that it is being picked-up and added to the system classloader.

At this point it would be useful to isolate the problem. Can you remove most jar files and simply write a small problem that reference this missing Java class? e.g simple create a simple class with main method creating a new instance of the class com.beans.MyBean and try to replicate similar to error Spring is getting.

Also, please zip the mybeans.jar and resend to phcharbonneau@hotmail.com if you can or share this file via any free file sharing site.

Dear Sir,I use to write J2EE project quite well but I'm a new comer of JADE. I want to combine J2EE and Multi-Agents for Information Retrieval from different Web services. I want to Know is JADE agent can call Web Services which are hosted in VM machine.

In fact,I want to publish this post with my testing code but because of limitation.

Can you please provide the full Java error and stack trace regarding java.lang.NoClassDefFoundError ? This will allow us to identify which Java class did not get loaded within the class loader/class path at runtime.

I need your kind assistance. I am able to run an applet on my local pc (development PC), but once I publish this on the server where I want it to be accessed I can't run the applet. I am getting the error NoClassDefFoundError.

Local CLASSPATH d:\java\jdk1.6.0\bin\mail.jar;d:\java\jdk1.6.0\bin\activation.jar;d:\java\jdk1.6.0\bin

Can you please include the full NoClassDefFoundError error and stack trace, including the Java class the JVM is complaining about. This will allow you to understand if you are dealing with a missing JAR file on the server side.

D:\Java\jdk1.6.0\bin>appletviewer SUAPLevel2.javajava.lang.NoClassDefFoundError: javax/mail/Address at SUAPLevel2.init(SUAPLevel2.java:533) at sun.applet.AppletPanel.run(AppletPanel.java:417) at java.lang.Thread.run(Thread.java:619)

hi sir,I have this NoClassDefFoundError in my applet when i try to embed a jar file into my webpage.This is my code.I have java 7 on my pc. code="Dotlet.class" codebase="..\dotplot1.5\" archive="dotlet.jar" width="378" height="380"

hi sir, i'm having trouble in using array descriptor with IBM Websphere Application Server Liberty Profile v8.5 as my server, like any other, it's regarding NoClassDefFoundError,here's the stackTrace:java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.apache.axis2.rpc.receivers.RPCUtil.invokeServiceClass(RPCUtil.java:194) at org.apache.axis2.rpc.receivers.RPCMessageReceiver.invokeBusinessLogic(RPCMessageReceiver.java:98) at org.apache.axis2.receivers.AbstractInOutMessageReceiver.invokeBusinessLogic(AbstractInOutMessageReceiver.java:40) at org.apache.axis2.receivers.AbstractMessageReceiver.receive(AbstractMessageReceiver.java:96) at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:145) at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:275) at org.apache.axis2.transport.http.AxisServlet.doPost(AxisServlet.java:120) at javax.servlet.http.HttpServlet.service(HttpServlet.java:595) at javax.servlet.http.HttpServlet.service(HttpServlet.java:668) at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1240) at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:760) at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:443) at com.ibm.ws.webcontainer.filter.WebAppFilterManager.invokeFilters(WebAppFilterManager.java:1033) at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:4499) at com.ibm.ws.webcontainer.osgi.DynamicVirtualHost$2.handleRequest(DynamicVirtualHost.java:282) at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:954) at com.ibm.ws.webcontainer.osgi.DynamicVirtualHost$2.run(DynamicVirtualHost.java:252) at com.ibm.ws.http.dispatcher.internal.channel.HttpDispatcherLink$TaskWrapper.run(HttpDispatcherLink.java:584) at com.ibm.ws.threading.internal.Worker.executeWork(Worker.java:439) at com.ibm.ws.threading.internal.Worker.run(Worker.java:421) at java.lang.Thread.run(Unknown Source)Caused by: java.lang.NoClassDefFoundError: oracle/sql/ArrayDescriptor

Morning P-H, i just finishing watching your video and i still cant resolve this problem.

Class not found com.pagatech.test.web.pc.agent.AgentSuiteRunnerjava.lang.ClassNotFoundException: com.pagatech.test.web.pc.agent.AgentSuiteRunner at java.net.URLClassLoader$1.run(URLClassLoader.java:372) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:360) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:693) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClasses(RemoteTestRunner.java:429) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Please kindly help me out and also it it possible through team viewer.