Tomcat 5.0.11 java.lang.ClassNotFoundException

kmthien

Senior member
Oct 8, 2002
363
0
0
Hi Guys,

I have installed Wrox web application under /usr/local/tomcat5/webapps/wrox

I can't fix the JavaBean I created under

/usr/local/tomcat5/webapps/wrox/chp4/Reuse/WEB-INF/classes/com/wrox/cars/CarBean.class

CarBean.java
======================================================
package com.wrox.cars;
import java.io.Serializable;

public class CarBean implements Serializable {
private String make = "Ford";
private double cost = 10000.00;
private double taxRate = 17.5;

public CarBean(){
}

public String getMake() {
return make;
}

public void setMake(String make) {
this.make = make;
}

public double getPrice() {
double price = (cost + (cost * (taxRate/100)));
return price;
}
}
======================================================

I created
/usr/local/tomcat5/webapps/wrox/chp4/Reuse/carPage.jsp

======================================================
<html>
<head>
<title>Using a JavaBean</title>
</head>
<body>

<h2>Using a JavaBean</h2>

<jsp:useBean id="myCar" class="com.wrox.cars.CarBean" />

I have a <jsp:getProperty name="myCar" property="make" /> <br />

<jsp:setProperty name="myCar" property="make" value="Ferrari" />

Now I have a <jsp:getProperty name="myCar" property="make" />

</body>
</html>
======================================================

But Tomcat return

exception
org.apache.jasper.JasperException: com.wrox.cars.CarBean
root caused
java.lang.ClassNotFoundException: com.wrox.cars.CarBean


when I tried to display carPage.jsp

Pls help !!
 

Argo

Lifer
Apr 8, 2000
10,045
0
0
My guess would be a problem w/ the classpath variable. You code looks fine.
 

Softballslug

Senior member
Feb 22, 2000
397
0
0
Here is the code you need for the JSP:
++++++++++++++++++++++++++++
<html>
<head>
<title>Using a JavaBean</title>
</head>
<body>

<h2>Using a JavaBean</h2>
<jsp:useBean id="myCar" class="com.wrox.cars.CarBean"></jsp:useBean>

I have a <%= myCar.getMake() %> <br />
<% myCar.setMake("Ferrari"); %>

Now I have a <jsp:getProperty name="myCar" property="make" />

</body>
</html>
++++++++++++++++++++++++

Either the <%= or the <jsp:getProperty will work as I have shown above...
 

kmthien

Senior member
Oct 8, 2002
363
0
0
Hi Softballslug,

I appreciate your help but the problem here is Tomcat unable to find the CarBean.class as it displayed the error message :

exception
org.apache.jasper.JasperException: com.wrox.cars.CarBean

root cause
java.lang.ClassNotFoundException: com.wrox.cars.CarBean


Thanks !!! Pls help again !!!

 

Softballslug

Senior member
Feb 22, 2000
397
0
0
I used your code and modified the classpath here: <jsp:useBean id="myCar" class="com.wrox.cars.CarBean" />

and put it into my tomcat application and it worked just fine. The error message is a jasper exception which means
the jsp compiler (jasper), and not Tomcat is unable to find the bean.

You never initialize your bean in the JSP either.

What is the context that you are using to reference your project?

Basically, I modified your jsp to look like the one I posted, placed it into my project, initialized the Bean properly
and your code worked just fine in my environment. I fully understand your problem however, once you figure out
the classpath issue above, you will get a null pointer exception ;)

Show me what your context is (its in tomcat5/conf/server.xml) and we can go from there.

 

kmthien

Senior member
Oct 8, 2002
363
0
0
Hi Softballslug,

Thanks a lot !! I added the following to /usr/local/tomcat5/conf/server.xml

===========================================================================
<Context path="/Wrox/chp4/Reuse" docBase="Wrox/chp4/Reuse" debug="0"
reloadable="true" crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="wrox_chp4_log." suffix=".txt"
timestamp="true"/>
</Context>

<Context path="/Wrox/chp6/Objects" docBase="Wrox/chp6/Objects" debug="0"
reloadable="true" crossContext="true">
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="wrox_chp6_log." suffix=".txt"
timestamp="true"/>
</Context>
===========================================================================

and it works fine now !!

However, I got a problem here and hope u can help !!

usr/local/tomcat5/webapps/Wrox/chp6/Objects/bookPage2.jsp

bookPage2.jsp
=================================================
<html>
<head>
<title>A Simple Class</title>
</head>

<body>
<%
booklibrary.Book myBook = new booklibrary.Book("Beginning JSP 2.0");
%>
Book Title:
<%= myBook.getTitle() %>
</body>
</html>
=================================================

/usr/local/tomcat5/WEB-INF/classes/booklibrary/Book.java
=================================================
package booklibrary;

public class Book
{

private String title;

public String getTitle()
{
return title;
}

public void setTitle(String newTitle)
{
this.title = newTitle;
}

// Uncomment this constructor for bookPage2.jsp (page 181)
public Book(String title)
{
this.title = title;
}
=================================================

When I tried to display the bookPage2.jsp, I got the following error :
=================================================
exception

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 7 in the jsp file: /chp6/Objects/bookPage2.jsp

Generated servlet error:
[javac] Compiling 1 source file

/usr/local/jakarta-tomcat-5.0.11/work/Catalina/localhost/Wrox/org/apache/jsp/chp6/Objects/bookPage2_jsp.java:47: package booklibrary does not exist
booklibrary.Book myBook = new booklibrary.Book("Beginning JSP 2.0");
^

An error occurred at line: 7 in the jsp file: /chp6/Objects/bookPage2.jsp

Generated servlet error:
/usr/local/jakarta-tomcat-5.0.11/work/Catalina/localhost/Wrox/org/apache/jsp/chp6/Objects/bookPage2_jsp.java:47: package booklibrary does not exist
booklibrary.Book myBook = new booklibrary.Book("Beginning JSP 2.0");
^
2 errors

org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:128)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:351)
org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:413)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:453)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:437)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:555)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:291)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:285)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:232)
javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
=================================================

How come bookPage2.jsp unable to access Book.class ?
Thanks a lot for your assistance !!

 

Softballslug

Senior member
Feb 22, 2000
397
0
0
Glad to help!

On your next question... Where does the classes for booklibrary exist in the tomcat structure??

You showed that the source was here:
/usr/local/tomcat5/WEB-INF/classes/booklibrary/Book.java

However, this should be the location of the classes and should look like:

/usr/local/tomcat5/WEB-INF/classes/booklibrary/Book.class
 

kmthien

Senior member
Oct 8, 2002
363
0
0
Hi,

Sorry for the confusion as the typed it wrongly. The Book.class is under
usr/local/tomcat5/webapps/Wrox/chp6/Objects/WEB-INF/classes/booklibrary/Book.java
 

Softballslug

Senior member
Feb 22, 2000
397
0
0
Since this is in a different folder:
usr/local/tomcat5/webapps/Wrox/chp6/Objects/WEB-INF/classes/booklibrary/Book.java

You need to have a different context for it since this project reference chp6 under Wrox and your
old context was referencing chp4.

 

kmthien

Senior member
Oct 8, 2002
363
0
0
Thanks a lot Softballslug !! I move the Book.class to /usr/local/tomcat5/webapps/wrox/WEB-INF/classes/booklibrary/Book.class and it works now !! Thanks !
 

Softballslug

Senior member
Feb 22, 2000
397
0
0
Again, glad to help! Breaks my boring days doing development work (Tomcat and JSP's...)

Best of luck!