FreeKB - Resolve NoClassDefFoundError in WebSphere
Resolve NoClassDefFoundError in WebSphere

Home > Search

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 WebSphere 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 People ${WAS_INSTALL_ROOT}/profiles/your_profile/installedApps/your_cell/your.ear/your.war/index.jsp

<%@page import="com.sample.main.People"%>

 

And you also should be able to locate the People.class file below com/sample/main.

ls ${WAS_INSTALL_ROOT}/profiles/your_profile/installedApps/your_cell/your.ear/your.war/WEB-INF/classes/com/sample/main/People.class

 


JAR file

Another common implementation is to create a JAR file that contains the class. In this example, a JAR file that contains the com.sample.main.People class could be created. You would then export the project to a JAR file and create a dynamic web project. There are a few options on how to get the dynamic web project to use the JAR file.

  • Place the JAR file in the applications WEB-INF/lib folder
  • Place the JAR file in the application servers classpath
  • Place the JAR in a shared library

Each of these options are described in more depth in this article. As long as you properly implement any of these options, NoClassDefFoundError should not be returned.

 

 

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter 8e064 in the box below so that we can be sure you are a human.




Comments