aspose file tools*
The moose likes Servlets and the fly likes using packages 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 "using packages" Watch "using packages" New topic
Author

using packages

Jenn Person
Ranch Hand

Joined: Jan 16, 2005
Posts: 89
Hi,
I'm making an online book store with the MVC pattern. I've made several beans to load and control user info and those are located in a package folder in my 'classes' directory. For example, classes/com/beans/myBean.class.
Now I'm making controller classes to accept requests and process them accordingly from the front end. These controller classes are in classes/com/controllers directory and need to access those beans. However when I try to import the classes like 'import com.beans.*;', I get an error saying the package and/or directory com/beans does not exist?? What am I doing wrong?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Is this happening when you try to compile or when you run the app?


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

Joined: Jan 16, 2005
Posts: 89
This is happening when I compile, it generates an error stating the directory is not found. Could it be because the beans folder and controllers folder are under the same parent directory?

EX. classes/com/controllers classes/com/beans
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Is the 'classes' directory on your classpath?
Are the source files for that package reachable via the classpath?
Jenn Person
Ranch Hand

Joined: Jan 16, 2005
Posts: 89
No it's not. I'm running Windows 98. I'm using Jakarta tomcat 4.0.1 and j2sdk1.4.0_02. Both tomcat and js2dk are installed on the C:/. I've added to my autoexec.bat file JAVA_HOME for my tomcat installation, and CATALINA_HOME for my j2sdk installation. As for my classpath, I have a added a library JAR file located within the j2sdk installation but that is it. Perhaps that's my problem?

my whole application is under the webapps directory within the jakarta-tomcat directory. Should I add the tomcat directoty to my classpath as well??
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Add the classes directory to your classpath.

Any time the compiler can't find a class or package, check to see that they're on your classpath.
Jenn Person
Ranch Hand

Joined: Jan 16, 2005
Posts: 89
ok so for instance... my application is called jTunes.

I would add c:/jakarta-tomcat-4.0.1/webapps/jTunes/WEB-INF/classes ??

Secondly, is this something I would have to do for any application? Could I just add the jakarta-tomcat-4.0.1 directory instead without having to be so directory specific?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Jenn Person:
ok so for instance... my application is called jTunes.
I would add c:/jakarta-tomcat-4.0.1/webapps/jTunes/WEB-INF/classes ??
Secondly, is this something I would have to do for any application? Could I just add the jakarta-tomcat-4.0.1 directory instead without having to be so directory specific?


This isn't really specific to webapps or Tomcat. It's a basic Java issue.
The java compiler finds your sources, compiled dependencies, and libraries by looking on the classpath. To compile your servlets you will need
A.) The classpath to point to the directory where your packages are
b.) The servlet libraries (either servlet.jar or servlet-api.jar, under the TOMCAT_HOME/common/lib dir, depending on your version of Tomcat)
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

So, in your case you will need to set your classpath to:
c:\jakarta-tomcat-4.0.1\webapps\jTunes\WEB-INF\classes;c:\jakarta-tomcat-4.0.1\common\lib\servlet.jar
Jenn Person
Ranch Hand

Joined: Jan 16, 2005
Posts: 89
Great, thank you so much, you've been a tremendous help!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using packages