File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Package confusion. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Package confusion." Watch "Package confusion." New topic
Author

Package confusion.

Rao Rekha
Greenhorn

Joined: Jul 29, 2002
Posts: 11
I have 3 classes class1, class2 and class3 in the package p1. class2 extends class1. All these classes contain package statement(not using import statement anywhere). In such a case, where do I place the class2 in my directory structure? If I place it inside the folder p1, compiler gives "file not found error" because it extends class1. But if I place it outside p1 folder, it compiles fine. But shouldn't I place a class in a folder named same as the package if there is a package statement specified? Could anyone clear my doubt. Thanks
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Placing classes within packages has nothing to do with inheritance. There is nothing that prevents you to put class1 and class2 within the same package even if class2 extends class1. You should have your three classes in directory p1 and it should compile fine. Go up one directory and type in the following command:
javac -classpath . p1\*.java


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Rao Rekha
Greenhorn

Joined: Jul 29, 2002
Posts: 11
Thanks Valentin.
I have tried that but no use. It just isn't recognizing my class1 either in class2 nor class3(where I am trying to create an instance of class1). It gives the error -
cannot resolve symbol
symbol : class class1
location: class p1.class1 ....
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Could you post the relevant code of your three classes as well as the command you use for compiling, please?
Dinesh Kumar
Ranch Hand

Joined: Jul 03, 2002
Posts: 54
Hi Valentin,
Why is it that when we compile the three classes from within the directory p1, the superclass as well as the other class compile but the subclass doesn't compile, whereas if we go up one level then all three compile?
Here's the code I used:

Thnx.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
It compiles fine for me :roll:
My directory structure is as follows:
In c:\test, I have the directory p1 with all three classes in it. Then I go up one level to c:\test and type the command:
javac -classpath . p1\*.java
and everything compiles and runs fine...
By executing
java -classpath . p1.AAA
I get the output:
HI AAA
Dinesh Kumar
Ranch Hand

Joined: Jul 03, 2002
Posts: 54
Yes Valentin, but the subclass doesn't compile if you get into p1 and then issue the compilation command on the individual java file (the sub-class). Whereas all three compile fine if you issue the compilation command "javac *.java". Why?
Look at the screen-shot below:

The sub-class file AA.java didn't compile individually but compiled ok with the "*.java" command. Isn't this strange?
Thnx.

Thnx.
[ August 02, 2002: Message edited by: Dinesh Kumar ]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
if you go into the package p1 and compile the subclass you have to give the classpath as ".." as follows:
javac -classpath .. AA.java
so that package p1 (in which the superclass is) can be found. Remember that the superclass is not A but p1.A and in order for A to be found, p1 must be found too.
Dinesh Kumar
Ranch Hand

Joined: Jul 03, 2002
Posts: 54
Ok. Valid point but how does it compile with the *.java command even when issued from within the directory "p1"? Is it that the *.java command first takes the file A.java and the compiler "knows" that it has A.class available so it lets AA.java compile trouble-free?
Can you pls. explain what might be happening behind the scenes in this case?
Thnx
 
GeeCON Prague 2014
 
subject: Package confusion.