/ java.lang.NoClassDefFoundError: How to resolve – Part 1 ~ Java EE Support Patterns

6.08.2012

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

Exception in thread "main" java.lang.NoClassDefFoundError is 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.

java.lang.NoClassDefFoundError – what is it?

** Tutorial video is now available: http://www.youtube.com/watch?v=Nn_Pq8cWiBg

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.

43 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.

Thanks Javin for our comments and sharing,

You are totally correct. Good knowledge on Classloaders, especially in the JavaEE space is very important.

P-H

It is very useful. Waiting for the next part.
Safik

Thanks Safik,

The part 2 will follow shortly.

Regards,
P-H

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.

how can i fix this?
Thanks in advance.

Hi Anonymous,

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.

Regards,
P-H

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

Hi Praneeth,

Can you please share the full StackTrace of your java.lang.noclassdeffounderror?

It's likely a classpath problem e.g. some code dependencies not properly added to the runtime classpath.

Thanks.
P-H

excellent web site with tons of useful information. Keep up the good work.

hi,

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/CustomersDAO
Caused 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)

Please help

Hi anonynous,

Can you please post the complete stack trace? e.g. you first need to identify the Java class referencing the com/sankya/itech/dao1/CustomersDAO

Please also locate in which JAR file the missing class is packaged and determine if your classpath is setup properly.

Thanks.
P-H

Hello P-H,

I've gone through your article as well as Javin Paul's article described in the very first comment. But I'm still not able to find solution. Can you help me?

Thanks in advance.

Hi Anonymous,

Sure, please describe your problem and post the error with full stacktrace so I can have a quick look.

Thanks.
P-H

I'm trying to run swt-java project from command prompt (without eclipse) & getting the following exception :

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.

Hi anonymous,

Did you packaged this class properly in your Jar and also added it to your start-up classpath?

P-H

Yes, I'm able to see it into my jar as well as in the list I've got by running the following command in linux :

jar tf mybeans.jar

I have added the same in .classpath. But it seems like it's not even reaching upto that point as I've tried changing its path but got the same error.

Hi anonymous,

At this point, please email me the command you used along with the JAR file itself and I will have a look:

phcharbonneau@hotmail.com

Thanks
P-H

Hello Sir,

Sorry but I'm not able to send you e-mail on this id: phcharbonneau@hotmail.com

I'm getting Mail Delivery System Failure.

Thanks.

Alright, so can you please please provide the following:

- Full start-up command you are using
- Open the JAR file and let me know the directory structure, do you see your MyBean.class under the com/beans/ directory structure?

P-H

I have made a file Startup.sh which contains the following data :

java -classpath .:swt.jar:jfacedb.jar:equinox.jar:coredb.jar:coreob.jar:hibernate3.jar:log4j.jar:comlog.jar:dom4j.jar:commons-collections.jar:gwt-servlet.jar:jtds-1.2.5.jar:cglib-2.1.3.jar:antlr-2.7.6.jar:asm-attrs.jar:asm.jar:beans-dm.jar:jta.jar:beanlib-hibernate-5.0.1beta.jar:gwt-crypto-1.0.3.jar:commons-beanutils-1.7.0.jar:beanlib-5.0.1beta.jar:commons-io-1.2.jar:beanlib-5.0.1beta-sources.jar:beanlib-hibernate-5.0.1beta-sources.jar:beanlib-joda-5.0.1beta.jar:business-interfaces.jar:commons-collections.jar:commons-digester-1.8.jar:commons-lang-2.4.jar:commons-logging-api.jar:DM-Beans-Utils.jar:DynamicJasper-2.0.8:groovy-all-1.5.5.jar:hibernate-annotations.jar:hibernate-commons-annotations.jar:hibernate-entitymanager.jar:hibernate-tools.jar:iText-2.1.5.jar:iText-rtf-2.1.5.jar:jasperreports-1.2.7.jar:jasperreports-3.5.1.jar:javassist.jar:jbossall-client.jar:jcommon-1.0.0.jar:mybeans.jar:slf4j-api-1.6.1.jar:sqljdbc4.jar:mail.jar:mysql-connector-java-5.1.6-bin.jar:jxl.jar:jdtcore-3.1.0.jar:jdo2-api-2.3-SNAPSHOT.jar:DM.zip MainForm.java

mybeans.jar contains MyBean.java.
Double clicking this file runs the project.
Apart from that I can see MyBean.class under mybeans.jar at the specified location.

Hi anonymous,

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.

Thanks.
P-H

Hello P-H,

Thanks for pointing my mistakes. I have improved that & also placed mybeans.jar at the very first. But still I'm getting the same error.

Thanks.

Hi anoynmous,

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.

Thanks.
P-H

Hello P-H,

I have figured out the problem. In my script you can see a line "MainForm.java" which points to the class contained main() method. There were 2 problems:

1. The class name should be fully qualified. In my case it should be com.MainForm.java.

2. When I write a dot, it means a folder structure. So I need to write com.MainForm only & not com.MainForm.java.

And it worked.
But thank you so much for your support.

Thanks anonymous,

Very often it is useful to start checking for simple mistakes vs. more complex root causes.

Regards,
P-H

hi P-H

i am running an application using java -cp command but i am getting no class definition found error can you pls help me?

I am using below command
"java -cp MapMatching.jar:lib/kd.jar com.ibm.MapMatching.HMMapMain -edge false"

Hi Uma,

Can you please post the entire NoClassDefFoundError along with the full stack trace?

Thanks.
P-H

Hello P-H,

I was having serious problems trying to set up a small Java application containing multiple jar files and after reading your articles everything turned clear to me. THANK YOU VERY MUCH.

Hi
How to add class to start-up classpath

Thanks Willy for your comments,

Happy to see that the article helped with your problem case.

Regards,
P-H

Hi anonymous,

The first question is how you are deploying your application? Are you using a standalone Java application, or Web application etc.?

Java classes and jar files can be added to the system classloader via the -classpath or -cp argument.

http://javaeesupportpatterns.blogspot.com/2011/08/jvm-options-hotspot-vm-standard-options.html

Thanks.
P-H

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.


WSAgent1 can properly create and start using JADE GUI. But cannot
start inside code (eg. calling function Searching(String doctorName,
String disease, String day, String time) inside interface agent.
I want know that why WSAgent1 can't properly start inside J2ee struts
project. Why java.lang.NoClassDefFoundError is encountered. Or Jade Agent cannot call Web Service in this way. I'm so nervous and very tired now.
I need your help sir. Give me a hand please.
Thanks in advance
Yu Mon Zaw

Hi Yu,

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.

Thanks.
P-H

Dear Sir,
I just wrote simple "helloworld" program... it compile fine but at runtime it gives same java.lang.NoClassDefFoundError... I already set the path it gives same error with any java program...

Thanks

Hi Anuj,

Can you please post the complete NoClassDefFoundError stack trace including the Java class that it is complaining about? Please also include your Java command you are using to execute your program.

Regards,
P-H

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

Server CLASSPATH C:\inetpub\SUAP\mail.jar; C:\inetpub\SUAP\activation.jar; C:\inetpub\SUAP; inetpub\SUAP

What am I doing wrong?

Hi Tirus,

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.

Regards,
P-H

JVM is complaining about the class javax.mail.Address

D:\Java\jdk1.6.0\bin>appletviewer SUAPLevel2.java
java.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 Tirus,

It is complaining about one class from mail.jar. It is obviously due to missing JAR file in your SERVER classpath. Can you please double check once again the classpath and this JAR file location?

I see drive C for the classpath but you are running the applet from the server drive D?

Thanks.
P-H

hi this is vasantha arise a problem like classnotfoundexception with org.hibernate.criteria and i tried to insert related jar files also its not working kindly solve my problem

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"

PARAM NAME="HELPURL" VALUE="input.txt"

Hi Vasantha,

Can you please post the complete stack trace of the ClassNotFoundException error?

Regards,
P-H

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

Post a Comment