
NoClassDefFoundError will be returned when an application is unable to load a class. In this example, the application is unable to load the "People" class in the "com.sample.main" package.
Let's take a simple example where the "People" class in the "com.sample.main" package and the JSP using the class exist in a single application. For example, here is the People class in the application.
package com.sample.main;
public class People {
public String name = "John Doe";
public String getName(){
return name;
}
}
And here is the JSP page in the same application using the People class.
<%@page import="com.sample.main.People"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<%
People foo = new People();
out.print(foo.getName());
%>
</body>
</html>
In this scenario, when the application is deployed to a Tomcat application server, NoClassDefFoundError should not be returned.
And the grep command (on a Linux server) should show that the applications JSP is indeed importing the com.sample.main.People class.
~]# grep -i People <Tomcat base directory>/webapps/my-app/index.jsp
<%@page import="com.sample.main.People"%>
JAR files | Search for the class
If an application is getting ClassNotFoundException, you will first need to determine how the classes in the JAR file are being made available to the Java application. The classes in a JAR file can be made available to a Java application running in a Tomcat application server in the following ways.
- The JAR file can be placed in the applications WEB-INF/lib folder
- The JAR file can be placed in the <base Tomcat directory/lib directory
Search for the class
On a Linux system, the following command can be used to find all of the files ending in .jar below the applications WEB-INF/lib directory, and to then use the Java jar command to check each JAR file for the class that is not found, and to print the results to files.txt.
files=$(find </path/to/WEB-INF/lib directory> | grep -i [A-Za-z0-9].jar$); for file in $files; do echo $file | tee -a files.txt; jar -tf $file | grep -i 'your class name here' | tee -a files.txt; done;
And you will probably want to also search the <base Tomcat directory/lib directory.
files=$(find <base Tomcat directory>/lib | grep -i [A-Za-z0-9].jar$); for file in $files; do echo $file | tee -a files.txt; jar -tf $file | grep -i 'your class name here' | tee -a files.txt; done;
Did you find this article helpful?
If so, consider buying me a coffee over at