• 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

package help

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
�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
 
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class Foo should go in com/staten/chris, a path that's relative to your classpath.
 
Chris Staten
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Chris -

Your reply made me thirsty!
 
Chris Staten
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ummmmmmm, beer good.


Chris banging his head aginst wall bad.
 
Jeff Bosch
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
LOL! (For newbies, that means "Hahahaha he-hee!")
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
BeerSelect.java




BeerExpert.java
 
Chris Staten
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Chris Staten
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
ice is for people that are not already cool. Chill with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic