aspose file tools*
The moose likes Servlets and the fly likes Error compiling a simple servlet !!.... any ideas?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Error compiling a simple servlet !!.... any ideas??" Watch "Error compiling a simple servlet !!.... any ideas??" New topic
Author

Error compiling a simple servlet !!.... any ideas??

Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Hi. I'm using tomcat and I created a new folder called project to develop my application. I have a form (jsp file) in which a user enters information. The information is validated using a javaBean (FormBean.java). If it is valid, a servlet connects to a DB (DBServlet.java) and checks whether username is already taken.
directory structure is somethin' like:
tomcat
....weapps
...project
.... jsp (all jsp files)
.... WEB-INF
.... classes
... project (all classes and Beans)
.... lib
the bean (FormBean.java) is working ok, but when I compile the servlet (DBServlet.java)
cannot access FormBean
class file contains wron class: project.FormBean
please remove or make sure it appears in the correct subdirectory of the classpath
import FormBean;
^
1 error
this is some of the servlet code

I've tried with
import project.FormBean;
but it doesn't work. What is wrong...???
thanks


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
1. You do have to use the full package name in the import statement.
2. When a servlet or JSP is compiled, the compiler is expecting to find the FormBean class in
TOMCAT-HOME/webapps/project/WEB-INF/classes/project/
Bill
[ March 14, 2002: Message edited by: William Brogden ]
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
Is project/WEB-INF/classes in your classpath? I have had similar problems as a result of not including my application in my classpath.


Matthew Phillips
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
No, I think I haven't included that yet. I'll do it when I get to Uni. I'll let you know guys what happens... thanks a lot
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Hi again.
I've added
c:\jakarta-tomcat-4.0.1\webapps\project\WEB-INF\classes;
to my classpath
Now, Should the import statement be like this:
import jakarta-tomcat-4.0.1.webapps.project.WEB-INF.classes.project.FormBean;
it retrieves an error in
jakarta-tomcat
^
; expected
Or, should it be like:
import TOMCAT_HOME.webapps.project.WEB-INF.classes.project.FormBean;
it retrieves an error in
; expected
WEB-INF
^
cannot resolve simbol
A much better directory structure (I hope) is like this one:
jakarta-tomcat-4.0.1
---webapps
-----project
-----jsp
-----WEB-INF
----classes
-----project
any ideas??? thanks
Michael Yuan
author
Ranch Hand

Joined: Mar 07, 2002
Posts: 1427
If c:\jakarta-tomcat-4.0.1\webapps\project\WEB-INF\classes is already in your class path (I think it is default for the JSp engine anyway ...), you only need to do
import project.*;
to access your beans.


Seam Framework: http://www.amazon.com/exec/obidos/ASIN/0137129394/mobileenterpr-20/
Ringful: http://www.ringful.com/
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Yes Michael. You are right. Thank you guys for your help.
now the code looks something like this:
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

I was confused for a second, until I realized the original problem was in compiling the servlet, not running it.

the WEB-INF folder is *known* to the servlet engine, and so does *not* need to be explicitly in the CLASSPATH ... except if you are using this directory for compiling your *.java source files as well.

And most people (myself being one) would argue that you should NOT be using system CLASSPATH for compiling. Set your classpath using the -classpath switch on javac command line instead, and you gain control of the compilation process, AND you avoid confusing run-time issues like "can't find right version of this class" (xml parsers come to mind)

In addition... you should not need the import project.*; statement in your servlet. You should never have to import classes that are in the same package of the class you are compiling (your servlet is in the project package, so you shouldn't need to import the project package to find your beans).

I suspect this was always a classpath issue, and not one of imports
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Thanks Mike for your comment. Besides getting a servlet or anything compiling and running, I think we should learn what the best practices are.
I am currently having my source files and classfiles in the same place, under:
c:\tomcathome\webapps\project\WEB-INF\classes\project
So, in this case my WEB-INF should be in the classpath, right?
Do you think I should only have my class files in that folder and the source files somewhere else?
If I decide to move the source files to another directory, how would the javac command be so the class files are placed directly under
c:\tomcathome\webapps\project\WEB-INF\classes\project
Or, How are you guys dealing with something like this? When I started using the -classpath switch I noticed that it was so much time consuming, that's the reason I'm using the CLASSPATH environment variable.
thanks again
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

The problem with using the system CLASSPATH variable is that other things use it as well.

There is currently a discussion underway (actually it might be in "Moderator's Only") that has the theme "Just say 'no' to classpath"

You are right, it is more time consuming to use the javac -classpath , but in the end, it is the much better way of doing things. Also.. most IDEs will handle classpath issues for you, and most of the *good* ones handle it by excusively using the -classpath switch.

As for moving the *.java files somewhere else.. I had asked that question a while back, and the response was basically "whatever you want, really". If you wanted to compile to a target directory though, from a command line, then using the -d switch tells the compiled classes where to go.

You might try something like this:From the source (src) directory:
d:\..\..\projects\src> javac -classpath . -d ..\WEB-INF\classes Servlet1.java
Should compile Servlet1.java and place Servlet1.class in the WEB-INF\classes directory.

The advantage of having a separate src directory is that it is very easy to exclude the source of your webapp from the distribution WAR file. Once you're done developing, simply move the directory to your 'archive place', and you're left with the application classes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error compiling a simple servlet !!.... any ideas??