Random Noclassdeffound Error in Web Application

ClassNotFoundException/NoClassDefFoundError in my Java web application


What does this mean?

First, let's see the meaning of java.lang.ClassNotFoundException:

Thrown when an application tries to load in a class through its string name using:

  • The forName method in class Class.
  • The findSystemClass method in class ClassLoader.
  • The loadClass method in class ClassLoader.

but no definition for the class with the specified name could be found.

Usually, this happens when trying to open a connection manually in this form:

String jdbcDriver = "...'; //name of your driver
Class.forName(jdbcDriver);

Or when you refer to a class that belongs to an external library and strangely this class cannot be loaded when the application server tries to deploy the application.

Let's see the meaning of java.lang.NoClassDefFoundError (emphasis mine):

Thrown if the Java Virtual Machine or a ClassLoader instance tries to load in the definition of a class (as part of a normal method call or as part of creating a new instance using the new expression) and no definition of the class could be found.

The searched-for class definition existed when the currently executing class was compiled, but the definition can no longer be found.

The last part says it all: the class existed at compile time i.e. when I compiled the application through my IDE, but it is not available at runtime i.e. when the application is deployed.



how can I fix it?

In Java web applications, all third party libraries used by your application must go in WEB-INF/lib folder. Make sure that all the necessary libraries (jars) are placed there. You can check this easily:

- <webapp folder>
- WEB-INF
- lib
+ jar1
+ jar2
+ ...
- META-INF
- <rest of your folders>

This problem usually arises for JDBC connectivity jars (MySQL, Derby, MSSQL, Oracle, etc.) or web MVC frameworks libraries like JSF or Spring MVC.

Take into account that some third party libraries rely on other third party libraries, so you have to add all of them in WEB-INF/lib in order to make the application work. A good example of this is RichFaces 4 libraries, where you have to download and add the external libraries manually.


Note for Maven users: you should not experience these problems unless you have set the libraries as provided, test or system. If set to provided, you're responsible to add the libraries somewhere in the classpath. You can find more info about the dependency scopes here: Introduction to the Dependency Mechanism


In case the library must be shared among several applications that will be deployed on your application server e.g. MySQL connector for two applications, there's another alternative. Instead of deploying two war files each with their own MySQL connector library, place this library in the common library folder of the server application, this will enable the library to be in the classpath of all the deployed applications.

This folder vary from application server.

  • Tomcat 7/8: <tomcat_home>/lib
  • JBoss 7/Wildfly: <jboss_home>/standalone/lib

Why am I getting a NoClassDefFoundError in Java?

This is caused when there is a class file that your code depends on and it is present at compile time but not found at runtime. Look for differences in your build time and runtime classpaths.

What is causing this NoClassDefFoundError error?

Are you doing something during the class initialization of MetricsRegistrar? Like code inside

static { 
...
}

Here someone had a NoClassDefError too because of this issue.

Why am I getting this NoClassDefFoundError?

I renamed out my .m2 directory and re-ran the maven build and this resolved the problem. Cache obviously got corrupted somehow.

Getting ServletException and NoClassDefFoundError during webapp startup

I assume that java.lang.NoClassDefFoundError: javax/persistence/Persistence is the root cause of the servlet instantiation error, so we can just ignore the servlet instantiation error for now. A NoClassDefFoundError means that the class mentioned in the message was present in the webapp's compile time classpath, but not in the webapp's runtime classpath.

Those JARs which you have there in /lib folder should actually go in /WEB-INF/lib. This is part of the webapp's runtime classpath. Further you should also remove those three jsp* and servlet* JAR files. They do not belong in the webapp library. They belong in the servletcontainer library. If you did this to fix compile errors, it has to be fixed differently.

NoClassDefFoundError from tomcat. is there something wrong with my pom?

java.lang.NoClassDefFoundError means the runtime version of the class in the classpath is not the same as that at compile time.

Your stacktrace does not have the ClassNotFoundError - so your problem could be multiple versions of the class being found when the server is deploying. ListableBeanFactory is part of spring-beans.jar

Can you search for how many versions of the spring-beans.jar are found in the run time server environment i.e. within Tomcat as part of the boorstrap you added and also within your own webapp libraries under WEB-INF?

May be there are multiples or the versions are clashing with each other.

Also looks like you're running Tomcat within your IDE in which case you'll have to search in the right location.



Related Topics



Leave a reply



Submit