File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes importing problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "importing problems" Watch "importing problems" New topic
Author

importing problems

Jenn Person
Ranch Hand

Joined: Jan 16, 2005
Posts: 89
Hi,
I'm using the MVC pattern. In my classes directory I have 3 subdirectories called beans, controllers, and models. My problem is that when writing servlets in my controller directory, I want them to be able to use classes from my models directory to access my database. However whenever I import my models directory and compile, I get a directory doesn't exist error. What am I doing wrong? Do I need to edit my classpath to include my models directory?

This is what I have in my servlets that are stored in my controllers directory.
ex. import models.*;

Thanks to any and all who can help me!!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You need to have the classes directory on your classpath.
Also, if you're not editing the source files, you need to have the root of your source tree on the classpath as well.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Nikhil Menon
Ranch Hand

Joined: Nov 22, 2004
Posts: 70
Hi,

Its a class path issue. Follow these steps.

(1) declare package models; in your model classes
(2) set the class path to the parent directory of your all packages. (that
is, say if you have controller,models,beans under 'src' directory. then
set classpath till 'src')
(3) Then compile all your classes (dont forget import models.*)
(4) Put you three packages under classes directory and start working

Make it a practice to create an src folder under WEB-INF and keep all you source codes under that. Keep only the .class files in the packages under WEB-INF/classes directory.

Let us know if it worked, if not give more details.

Nikhil
Jenn Person
Ranch Hand

Joined: Jan 16, 2005
Posts: 89
That's great advice, I'll make sure to put my .class files in the directories and leave the actual .java files in a src folder. I'll follow the steps and hopefully that will solve everything, I'll post a follow up soon.

Thanks again to both of you!!
Jenn Person
Ranch Hand

Joined: Jan 16, 2005
Posts: 89
Alright, I've moved all my java files to a src folder under my classes directory. The .class files are kept under their appropriate folders ie. beans in the beans folder, models in models folder, etc.

I've recompiled a few and they all say that the imported directories don't exist. I'm running on an old computer... Win98, not second edition, using tomcat. I've set JAVA_HOME, CATALINA_HOME and classpath according to what I've been told to through my autoexec.bat file but things still aren't working. Any advice? Should I post the contents of my autoexec.bat file so you can see what I've done thus far?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Post your CLASSPATH entry, the full path to your src files, and the full path to your class files.
Jenn Person
Ranch Hand

Joined: Jan 16, 2005
Posts: 89
Originally posted by Ben Souther:
Post your CLASSPATH entry, the full path to your src files, and the full path to your class files.


CLASSPATH:
SET CLASSPATH=c:\jakarta-tomcat-4.0.1\common\lib\servlet.jar;c:\jakarta-tomcat-4.0.1\webapps\jTunes\WEB-INF\classes

FULL PATH TO SRC FILES:
C:\jakarta-tomcat-4.0.1\webapps\jTunes\WEB-INF\classes

FULL PATH TO .CLASS FILES:
C:\jakarta-tomcat-4.0.1\webapps\jTunes\WEB-INF\classes\infostore\beans
C:\jakarta-tomcat-4.0.1\webapps\jTunes\WEB-INF\classes\infostore\controllers
C:\jakarta-tomcat-4.0.1\webapps\jTunes\WEB-INF\classes\infostore\models

PATH OF J2SDK INSTALLATION:
C:\j2sdk1.4.2_02

PATH OF JAKARTA-TOMCAT INSTALLATION:
C:\jakarta-tomcat-4.0.1
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

put your source files in:
C:\jakarta-tomcat-4.0.1\webapps\jTunes\WEB-INF\classes\infostore\beans

The directory structure needs to match your pacakge.
Do the same for the other two packages.

The simplest thing to do is to put each src files exactly where the class files will go.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Originally posted by Jenn Person:
import models.*;


Originally posted by Jenn Person:
C:\jakarta-tomcat-4.0.1\webapps\jTunes\WEB-INF\classes\infostore\models


If your classes are in the (classpath)\infostore\model directory, they should be packaged as
package infostore.model

and imported as
import infostore.model.*

This doesn't appear to be what you are saying above.
Jenn Person
Ranch Hand

Joined: Jan 16, 2005
Posts: 89
So long story short, I've left all my source codes in the corresponding directories their class files are in, but I've also put copies of each java file in the main classes directory. Compiling my servlets now allows me to import classes from other packages. Is this a common practice I should follow from now on??
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You shouldn't need to have copies of you java source files at all.
Nikhil Menon
Ranch Hand

Joined: Nov 22, 2004
Posts: 70
hi Jenn Person ,

It is not recommended to keep your source file under your 'classes'. While you develop the application you can keep the source files under 'src' folder then after compiling copy the same directory structure under the 'src' folder to under 'classes' folder, retain only '.class' files. And once deployed you should have only the '.class' files under your 'classes' folder and '.java' under the 'src'. If you have any '.jar' files keep them under '/WEB-INF/lib' folder. Thats it.

Nikhil Kanjulli Menon
SCJP1.4 (95%)
Jenn Person
Ranch Hand

Joined: Jan 16, 2005
Posts: 89
Alright I think I've finally got this under control. Just one last question, I'm using tomcat which I guess is obvious by now, is there another JVM that I should be using, perhaps that wouldn't require me to stop and start it everytime I make an update?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Look at the "reloadable" attribute of your context. When set to true, Tomcat will reload the classes every time you update them.
Jenn Person
Ranch Hand

Joined: Jan 16, 2005
Posts: 89
Hey Ben,

I'm not sure I know what you mean... where do I find this reloadable feature and how do I set it to true?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The context entry is in your TOMCAT_HOME/conf/server.xml file.

I believe for 4x, there is already one in there for the ROOT app.
You can just copy, paste, and edit to create another entry for your jTunes app.

You will be manually deploying your app instead of letting Tomcat do it automatically.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: importing problems