File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Why is this error? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Why is this error?" Watch "Why is this error?" New topic

Why is this error?

Arun, Shanmugham
Ranch Hand

Joined: Apr 05, 2001
Posts: 34
I have created a Simple Bean class. The class is not in a package. When i tried using it . I got the error that
"Class not found". But the same is working after bring it in a package. Will the bean work without package.

Sun Certified Programmer for Java2 Platform
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
It is my understanding the Beans' have to be in a package inorder for them to least with the Tomcat container. I don't know about other web servers

So much trouble in the world -- Bob Marley
Arun, Shanmugham
Ranch Hand

Joined: Apr 05, 2001
Posts: 34
Thanks Bosun
hope i will get some more detail on it.

Phil Hanna
Ranch Hand

Joined: Apr 05, 2001
Posts: 118
Bosun is correct, but this is really a bug in Tomcat.
The Tomcat (Jasper) JSP container generates servlet source code for your JSP. As part of this, it generates a "package" statement at the top, one that maps to your directory structure. However, this means if you use an unqualified class name in the code anywhere, it is assumed to be either (1) part of the same package as the generated JSP, or (2) found in the import list. Since your bean is neither, you get a compilation error.
There is an easy workaround, however. You can explicitly specify your bean in a page import directive:
<%@ page import="MyBean" %>
<jsp:useBean id="x" class="MyBean"/>
It's a little easier to see what I mean if you look at the generated servlet source code in your Tomcat/work directory.
I would emphasize again that this restriction is not part of the JSP specification, it is a Tomcat bug. JRun 3.0 has no such problem, for example, because it doesn't create a package statement.
Phil Hanna
Sun Certified Programmer for the Java 2 Platform
Author of :
JSP: The Complete Reference
Instant Java Servlets

Phil Hanna<BR>Sun Certified Programmer for the Java 2 Platform<BR>Author of :<BR><A HREF="" TARGET=_blank rel="nofollow">JSP: The Complete Reference</A><BR><A HREF="" TARGET=_blank rel="nofollow">Instant Java Servlets</A>
Arun, Shanmugham
Ranch Hand

Joined: Apr 05, 2001
Posts: 34
Thanks Phil, I got ur Point. May be i'll use JRun in case needed.
wood burning stoves
subject: Why is this error?
Similar Threads
Error : class not found while running jsp
Where to place JCE1.2.1 , which is used by my useBean class ?
JSP java.lang.classnotfoundexception
Is it mandatory to put a bean inside a package?
Method Not found in JSP usning a Bean and Tomcat