"NoClassDefFoundError" Exception

This section describes the compilation error on a JSP page that tries to use a JavaBean class, but the class bytecode is not accessible. You will get a NoClassDefFoundError Java exception or a JSP to Servlet class conversion error.

One common issue involved in the use JavaBean classes in JSP pages is the JavaBean class bytecode files () are not accessible by the Tomcat server. You can follow these steps to experience this issue:

1. Shut down the Tomcat server and remove TempratureConvertorBean.class file from the Tomcat server:

C:\>\local\apache-tomcat-7.0.32\bin\shutdown.bat

C:\>del \local\apache-tomcat-7.0.32\webapps\ROOT\WEB-INF\classes
   \herong\TempratureConvertorBean.class

2. Start the Tomcat server and view the JSP page:

C:\>\local\apache-tomcat-7.0.32\bin\startup.bat

C:\>type \local\apache-tomcat-7.0.32\webapps\ROOT
   \TempratureConvertor.jspx

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1">
<!-- TempratureConvertor.jspx
 - Copyright (c) 2012, HerongYang.com, All Rights Reserved.
-->
<jsp:directive.page contentType="text/html"/>
<html><body>
<jsp:useBean id="b" class="herong.TempratureConvertorBean"/>
<jsp:expression>b.getInfo()</jsp:expression><br/>
Setting property "fahrenheit" to 70.0.<br/>
<jsp:setProperty name="b" property="fahrenheit" value="70.0"/>
Getting property "celsius" back: 
<jsp:getProperty name="b" property="celsius"/>
<br/>
</body></html>
</jsp:root>

3. Visit http://localhost:8080/TempratureConvertor.jspx with a browser. The "NoClassDefFoundError" Tomcat error will show up:

HTTP Status 500 - javax.servlet.ServletException: 
java.lang.NoClassDefFoundError: herong/TempratureConvertorBean

type Exception report

message javax.servlet.ServletException: 
   java.lang.NoClassDefFoundError: herong/TempratureConvertorBean

description The server encountered an internal error 
   that prevented it from fulfilling this request.

exception
org.apache.jasper.JasperException: javax.servlet.ServletException: 
   java.lang.NoClassDefFoundError: herong/TempratureConvertorBean
   org.apache.jasper.servlet.JspServletWrapper.handleJspException()
   org.apache.jasper.servlet.JspServletWrapper.service()
   org.apache.jasper.servlet.JspServlet.serviceJspFile()
   org.apache.jasper.servlet.JspServlet.service()
   javax.servlet.http.HttpServlet.service()

root cause

javax.servlet.ServletException: 
   java.lang.NoClassDefFoundError: herong/TempratureConvertorBean
   org.apache.jasper.runtime.PageContextImpl.doHandlePageException()
   org.apache.jasper.runtime.PageContextImpl.handlePageException()
   org.apache.jsp.TempratureConvertor_jspx._jspService()
   org.apache.jasper.runtime.HttpJspBase.service()
   javax.servlet.http.HttpServlet.service()
   org.apache.jasper.servlet.JspServletWrapper.service()
   org.apache.jasper.servlet.JspServlet.serviceJspFile()
   org.apache.jasper.servlet.JspServlet.service()
   javax.servlet.http.HttpServlet.service()

4. Do another test by adding the "page import" element in the JSP page:

C:\>edit \local\apache-tomcat-7.0.32\webapps\ROOT
   \TempratureConvertorModified.jspx

<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1">
<!-- TempratureConvertorModified.jspx
 - Copyright (c) 2012, HerongYang.com, All Rights Reserved.
-->
<jsp:directive.page contentType="text/html"/>
<jsp:directive.page import="herong.TempratureConvertorBean"/>
<html><body>
<jsp:useBean id="b" class="herong.TempratureConvertorBean"/>
<jsp:expression>b.getInfo()</jsp:expression><br/>
Setting property "fahrenheit" to 70.0.<br/>
<jsp:setProperty name="b" property="fahrenheit" value="70.0"/>
Getting property "celsius" back: 
<jsp:getProperty name="b" property="celsius"/>
<br/>
</body></html>
</jsp:root>

5. Visit the modified JSP page. Interestingly, a new error will show up. This time is a JSP to Servlet class conversion error:

HTTP Status 500 - /TempratureConvertorModified.jspx 
   () The value for the useBean class attribute 
   herong.TempratureConvertorBean is invalid.

type Exception report

message /TempratureConvertorModified.jspx () 
The value for the useBean class attribute 
herong.TempratureConvertorBean is invalid.

description The server encountered an internal error that prevented it 
from fulfilling this request.

exception
org.apache.jasper.JasperException: 
   /TempratureConvertorModified.jspx () 
   The value for the useBean class attribute 
   herong.TempratureConvertorBean is invalid.
   org.apache.jasper.compiler.DefaultErrorHandler.jspError()
   org.apache.jasper.compiler.ErrorDispatcher.dispatch()
   org.apache.jasper.compiler.ErrorDispatcher.jspError()
   org.apache.jasper.compiler.Generator$GenerateVisitor.visit()
   org.apache.jasper.compiler.Node$UseBean.accept()
   org.apache.jasper.compiler.Node$Nodes.visit()
   org.apache.jasper.compiler.Node$Visitor.visitBody()
   org.apache.jasper.compiler.Generator$GenerateVisitor.visit()
   org.apache.jasper.compiler.Node$UninterpretedTag.accept()
   org.apache.jasper.compiler.Node$Nodes.visit()
   org.apache.jasper.compiler.Node$Visitor.visitBody()
   org.apache.jasper.compiler.Generator$GenerateVisitor.visit()
   org.apache.jasper.compiler.Node$UninterpretedTag.accept()
   org.apache.jasper.compiler.Node$Nodes.visit()
   org.apache.jasper.compiler.Node$Visitor.visitBody()
   org.apache.jasper.compiler.Node$Visitor.visit()
   org.apache.jasper.compiler.Node$JspRoot.accept()
   org.apache.jasper.compiler.Node$Nodes.visit()
   org.apache.jasper.compiler.Node$Visitor.visitBody()
   org.apache.jasper.compiler.Node$Visitor.visit()
   org.apache.jasper.compiler.Node$Root.accept()
   org.apache.jasper.compiler.Node$Nodes.visit()
   org.apache.jasper.compiler.Generator.generate()
   org.apache.jasper.compiler.Compiler.generateJava()
   org.apache.jasper.compiler.Compiler.compile()
   org.apache.jasper.compiler.Compiler.compile()
   org.apache.jasper.compiler.Compiler.compile()
   org.apache.jasper.JspCompilationContext.compile()
   org.apache.jasper.servlet.JspServletWrapper.service()
   org.apache.jasper.servlet.JspServlet.serviceJspFile()
   org.apache.jasper.servlet.JspServlet.service()
   javax.servlet.http.HttpServlet.service()

Last update: 2012.

Table of Contents

 About This Book

 JSP (JavaServer Pages) Overview

 Tomcat 7 Installation on Windows Systems

 JSP Scripting Elements

 Java Servlet Introduction

 JSP Implicit Objects

 Syntax of JSP Pages and JSP Documents

 JSP Application Session

 Managing Cookies in JSP Pages

JavaBean Objects and "useBean" Action Elements

 What Is a JavaBean?

 "jsp:useBean" Action Elements

 Servlet Class Converted from UseBean.jspx

 Setting and Getting JavaBean's Properties

 Using JavaBean Objects in Scripting Elements

 Using Java Objects as JavaBeans

 getProperty() Error on Tomcat 7

 Refreshing Loaded JavaBean Classes

 Importing Unnamed Package Class Error

 Using JavaBean without Import Element Error

 Creating JavaBean Classes in Named Packages

"NoClassDefFoundError" Exception

 Managing HTTP Response Header Lines

 Non-ASCII Characters Support in JSP Pages

 Performance of JSP Pages

 EL (Expression Language)

 Overview of JSTL (JSP Standard Tag Libraries)

 JSTL Core Library

 JSP Custom Tags

 JSP Java Tag Interface

 Custom Tag Attributes

 Multiple Tags Working Together

 File Upload Test Application

 Outdated Tutorials

 References

 PDF Printing Version