• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

importing problems

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Post your CLASSPATH entry, the full path to your src files, and the full path to your class files.
 
Jenn Person
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You shouldn't need to have copies of you java source files at all.
 
Nikhil Menon
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic