• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Package confusion.

 
Rao Rekha
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rao Rekha
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you post the relevant code of your three classes as well as the command you use for compiling, please?
 
Dinesh Kumar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic