This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes package help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "package help" Watch "package help" New topic
Author

package help

Chris Staten
Ranch Hand

Joined: Sep 24, 2004
Posts: 101
Hey all, I�m a little confused about packages.

Shouldn�t it work if I have a class named Foo with a package statement of say �package com.staten.chris;�, and I have another class named Bar with an import statement of �import com.staten.chris.Foo.*�?

My directory structure looks like this:
source files are in root/src/com/staten/chris and my class files are in root/classes/com/staten/chris.

I�m compiling from my root folder with the following commands:
For the Foo Class:
javac �classpath path/to/common/java/library �d classes src/com/staten/chris/Foo.java

For the Bar class:
javac �classpath path/to/common/java/library �d classes src/com/staten/chris/Bar.java


When I compile the Bar.java class I get a �package com.staten.chris.Foo.* does not exist� error. I really want to be able to use this Foo.class file.
[ October 25, 2004: Message edited by: Chris Staten ]
Amit Saini
Ranch Hand

Joined: Oct 20, 2004
Posts: 280
�import com.staten.chris.Foo.*�

This is incorrect.
THe correct way to import packages is

import packagename.* (import all classes within a specific package) OR
import packagename.classname (import only a specific package)

in your case you are doing import packagename.classname.*

Hope this helps.
Amit
Nigel Browne
Ranch Hand

Joined: May 15, 2001
Posts: 673

This statement is trying to import all classes in a directory entitled Foo, this doesn't exist hence the error. If you just want to reuse the functionality in class Foo, change your inport statement as follows:
Chris Staten
Ranch Hand

Joined: Sep 24, 2004
Posts: 101
Where does my package get put when I create it? This might help me access it. Does it get put in the classes/com/staten/chris directory, or is it somewhere else?

I'm thinking that my -classpath path may be wrong in my javac statement.



Edit:
I've changed my import statement to "import com.staten.chris.Foo;".
[ October 25, 2004: Message edited by: Chris Staten ]
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Class Foo should go in com/staten/chris, a path that's relative to your classpath.


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Chris Staten
Ranch Hand

Joined: Sep 24, 2004
Posts: 101
OK, this topic may get moved over to the JSP or the Servlets forums because of this post, but in my original post I was just trying to understand java package and import statements in general. Anyway�

Here is the exact syntax of my code and the javac commands. I�m trying to work an example out of Head First Servlets and JSP (chapter 3, pp80-85).

Package statement syntax:
package com.example.model;
This package statement is inside a file named BeerExpert.java

Import statement syntax:
import com.example.model.*;
This import statement is inside two different files BeerSelect.java and ModelTest.java.

My directory structure looks like this:

BeerExpert.java:
C:code/java/myProjects/beerV1/src/com/example/model

BeerSelect.java:
C:code/java/myProjects/beerV1/src/com/example/web

ModelTest.java:
C:code/java/myProjects/beerV1/src/com/example/model

I�ve created batch files for compiling my .java files, here are the commands:

BeerExpert.java:
javac -classpath ../../../../../../../"Program Files"/"Apache Software Foundation"/"Tomcat 5.0"/common/lib/servlet-api.jar -d classes src/com/example/model/BeerExpert.java

BeerSelect.java:
javac -classpath ../../../../"Program Files"/"Apache Software Foundation"/"Tomcat 5.0"/common/lib/servlet-api.jar -d classes src/com/example/web/BeerSelect.java

ModelTest.java:
javac -classpath ../../../../../../../"Program Files"/"Apache Software Foundation"/"Tomcat 5.0"/common/lib/servlet-api.jar -d classes src/com/example/model/ModelTest.java

I compile BeerExpert first and it compiles (this should create the package). It also creates the directory structure com/example/model where it places the BeerExpert.class file. When I try to compile BeerSelect.java or ModelTest.java they both come up with the �package does not exist� error.
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
Hi, Chris -

Your reply made me thirsty!
Chris Staten
Ranch Hand

Joined: Sep 24, 2004
Posts: 101
ummmmmmm, beer good.


Chris banging his head aginst wall bad.
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
LOL! (For newbies, that means "Hahahaha he-hee!")
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Chris Staten:
import com.staten.chris.Foo.*
As Amit and Nigel pointed out, this statement is incorrect. From your description, each class should start with the following.Note: the import line (1) is unnecessary since ModelTest is already in the same package (each class has access to all other classes in its own package).

One thing you can do is to compile all three classes at once by adding the other two .java files to the first command line.
Chris Staten
Ranch Hand

Joined: Sep 24, 2004
Posts: 101
OK, I changed my ModelTest.java source code to include the package statement:
package com.example.model;

Now I get one less error, I no longer get the �package does not exist� error. But I still get a ton of errors because the compiler doesn�t see the .class files (I think).

For example, I have the following statement:


Which produces the following error:



I�m going to post the code to my BeerExpert.java and BeerSelect.java files in the following post.
Chris Staten
Ranch Hand

Joined: Sep 24, 2004
Posts: 101
BeerSelect.java




BeerExpert.java
Chris Staten
Ranch Hand

Joined: Sep 24, 2004
Posts: 101
I really can't seem to find a problem with my code - obviously there's one there, but I can't find it. Since I copied the code I think I'm just going to move my entire project out of my dev and deployment directories and start from scratch, maybe I won�t duplicate my error. If anyone has any ideas please speak now or forever hold your peace
Chris Staten
Ranch Hand

Joined: Sep 24, 2004
Posts: 101
I�m sure that the problem is that my program is not finding the BeerExpert class files. If you look above I�ve only directed my �classpath option to look at the Tomcat common library files. I need it to also access my class file(s) that I created.


So I guess that one question I should ask is:
When using the �classpath flag, if I want to pull files from multiple directories (when compiling from the command line in Windows XP) what delineator do I need to use? Is it a colon or a semicolon?

Here is the new javac command that I�m going to use for the BeerSelect.java file, I added the "classes/com/example/model/BeerExpert.class" path to the -classpath flag.

javac -classpath /"Program Files"/"Apache Software Foundation"/Tomcat/common/lib/servlet-api.jar:classes/com/example/model/BeerExpert.class -d classes src/com/example/model/BeerSelect.java
[ October 26, 2004: Message edited by: Chris Staten ]
Chris Staten
Ranch Hand

Joined: Sep 24, 2004
Posts: 101
Got it, I took the time to look at the java beginners faq link a the top of the forum page and found the answer here: How To Set The Classpath

Sorry for all the posts on something that I could/should have found the answer to on my own.
[ October 26, 2004: Message edited by: Chris Staten ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
I'm glad you figured it out. Thanks for telling us about what you learned.

If you still have any questions about understanding the whole package and CLASSPATH thing, just ask. I do note that a number of incorrect statements and suggestions were made in various posts in this thread.

As I was beginning to learn Java programming and understanding packages and the CLASSPATH, it took me a little while before I succumbed to using and understanding the command prompt and doing things "manually" again. I'm glad I finally made the effort, as understanding these configuration issues has been very useful over the past few years.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Chris Staten
Ranch Hand

Joined: Sep 24, 2004
Posts: 101
Thanks for the words of encouragement Dirk.

BTW, here is the final (and working) version of my javac command:

The two things that I needed to change at the end were:
1. Using a semicolon to delineate instead of a colon.
2. I was specifying a path too far into my directory that had my class files. I was going into the part of my directory that was part of my package name. If you look at the final version of my javac command you see that I stop at the classes directory.
Guilherme Lopes Morais
Greenhorn

Joined: Oct 07, 2005
Posts: 1
Chris Staten...
Thank a LOT ... your final line for that command was very helpfull. Because in the Book they use : not ; for add another path, so this little difference become a HUGE Difference.

Thank you again.
[ March 15, 2006: Message edited by: Guilherme Lopes Morais ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: package help
 
Similar Threads
Chapter 10 Developement
basic doubt!!
jvm can't find class in same package
How to set up Java Classpath?
Package problem in walkthrough