Big Moose Saloon
 Search | Java FAQ | Recent Topics Register / Login

# Package visibility problem

arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 304
Hello,
It seems interesting to me to know how to compile the following codes. both java files is in the same directory. So it is something like this:
1. Say i have a dir called c:\test
2. my TestAnother.java nd One.java is laying int the above directory.
3. Now if i Try to compile TestAnother after compiling One.java it failed and give me error:

4. I also tried putting One.class in classpath and than compiling TestAnother but same result.
5. The other answer is to put the package on and than run. It works very well.
But seems more interesting to know if don't want to put pacakge what will be the solution for it?
regards,
Arun

[ edited to make small change in format -ds ]
[ August 25, 2002: Message edited by: Dirk Schreckmann ]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
4. I also tried putting One.class in classpath and than compiling TestAnother but same result.
That would be an incorrect solution. The correct solution would be to put the package location in your classpath. Which is maybe what you meant by this.
5. The other answer is to put the package on and than run. It works very well.
What exactly do you mean by the following?
But seems more interesting to know if don't want to put pacakge what will be the solution for it?

arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 304
Ok I think I failed to explain. Let me try it again step by step.
1. I have a directory called c:\test and have following in this directory
One.java
One.class
TestAnother.java

2. In One.java file there is no package mentioned and that is the reason why One.class is in the directory c:\test only.
3. In TestAnother.java I have the first line declaring the package as package vpet;. So that means I want to have TestAnother.class in the sub directory of vpet under test. That means it should be at c:\test\vpet.
4. Now when I compile the TestAnother.java it gives the above error as mentioned above in my first message. It does not able to find the One.class file.
5. It is most likely that the compiler is virtually sitting at the c:\test\vpet and looking for the One.class which is one step down and failed to find.
6. As in One.class I haven't define any package, it seems that I cannot even put in classpath. Atleast I could not able to make a syntax for it.
7. If I would have tried the above scenario by putting a package in the One.java file say package test; The One.class would be at c:\test\test. and I can easily import it into TestAnother.java by import clause like import test.*; and I am able to compile the TestAnother.java.
8. But the problem here is that I don't want to put One.class in any package but would like to put TestAnother.class in some package as mentioned above.
And to my horror I failed to resolve this issue.I hope I was able to make it more clear.
Would be obliged if someone show me the path on it.
regards,
arun
arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 304
regards,
arun
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
4. Now when I compile the TestAnother.java it gives the above error as mentioned above in my first message. It does not able to find the One.class file.
From exactly what directory do you use exactly what command?
e.g. c:\temp> javac -classpath somePath Whatever.java
7. If I would have tried the above scenario by putting a package in the One.java file say package test; The One.class would be at c:\test\test. and I can easily import it into TestAnother.java by import clause like import test.*; and I am able to compile the TestAnother.java.
Ditto?
arun mahajan
Ranch Hand

Joined: Dec 07, 2001
Posts: 304
Ok Dirk, I am trying from the directory test so it is like this:
my java files are in c:\test and this contains fllowing files:
One.java & TestAnother.java
TO Compile One.java I have issued following command:
c:\test>javac one.java
This command is no problem. Also to mention this class has no package
To Compile TestAnother.java I have issued following command:
c:\test>javac TestAnother.java
Also tried :
c:\test>javac -classpath .;c:\test\; TestAnother.java
Just to mention TestAnother.java has a package line at top i.e. package vpet;
Arun
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Seemingly annoying isn't it?
Sun's compiler isn't the only one that won't compile TestAnother.java in your example structure. Jikes also won't compile it.
Curiously, TestAnother.java can be compiled if it's in a subdirectory named vpet and the package declaration is removed (the working directory must be the same directory where One is located - this does not have to be the same directory where TestAnother.java is).
I don't fully understand why it won't work.

I agree. Here's the link: http://aspose.com/file-tools

subject: Package visibility problem