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.*;
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.
(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
posted
0
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
posted
0
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?
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
posted
0
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
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
posted
0
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??
You shouldn't need to have copies of you java source files at all.
Nikhil Menon
Ranch Hand
Joined: Nov 22, 2004
Posts: 70
posted
0
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
posted
0
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?