jQuery in Action, 2nd edition
The moose likes Beginning Java and the fly likes Packaging problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Packaging problem" Watch "Packaging problem" New topic

Packaging problem


Joined: Feb 02, 2003
Posts: 3
I have some problem about the package statement. For example, in the following three classes with definition:
package chapter1;
public class CharStack {
} // End of class CharStack
package chapter1;
public class PrintableCharStack extends CharStack {
} // End of class PrintableStack
package chapter2;
import chapter1.*;
public class Client {

public static void main(String[] args) {
PrintableCharStack a = new PrintableCharStack(3);

} // End of class Client
If I omit for package statement from the class Client, it will fail to compile with error "file does not contain class PrintableCharStack" even I import the package chapter1. However, if I add the package statement (package chapter2), it will compile and run correctly.
My question is why such an error would occur. Any impact on the package statement from the above case.
Tim B.

Joined: Feb 03, 2003
Posts: 10
Hi C WL,
It is correct that the compiler fails to compile when you omit the package statement in the Client object. This because the package statement can be seen as the directory in which the file is stored.
So when you omit the package statement in the Client file, it thinks the directory of the file is actually the root directory. Importing chapter1.* will therefore look for a directory 'chapter1' in the same directory (/chapter1/chapter2/*) as the Client.java file. When you include the package statement, it knows the actual path to the file is '/chapter2/Client.java' and therefore will look for the 'chapter1' directory one level lower than the Client.java source file is in.
Hopefully you are able to follow the above explanation. If not, feel free to post a question for more information and I'll try to be a bit more specific then.
rocket mass heater kickstarter
subject: Packaging problem