This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes packages and classpath! or at least i guess! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "packages and classpath! or at least i guess!" Watch "packages and classpath! or at least i guess!" New topic
Author

packages and classpath! or at least i guess!

Melanie Parker
Greenhorn

Joined: Jun 14, 2003
Posts: 12
Hello,
can someone help me with this package question please:
i have the following directory structure: C:\pack1\pack2\
inside pack2 i have the following source files:
Class1.java:
package pack1.pack2;
public interface Class1
{}
Class2.java:
package pack1.pack2;
public class Class2 implements Class1
{}
i can compile Class1.java using 'c:\pack1\pack2\javac Class1.java' with no problems.
afther that if i try and compile Class2.java using 'c:\pack1\pack2\javac Class2.java'
i get an error that the symbol Class1 cannot be resolved.
why is that? arent they in the same package? why cant't the class be found?
I know this has to do with classpath and packages but am not sure how.
I found a solution that is the following: 'c:\pack1\pack2\javac -classpath c:\ Class2.java'
which works without problems. so setting the classpath that way solves it. But shouldnt'
it work without needing to do that.
If i remove the package statements at both files all compile with no problems.
I never used package statements! and its quite frustrating now.
Can someone please explain to me why the first attempt fails, the second works.
or point me to an explanation on the net. The java tutorial doesnt address this directly,
it only mentions how to import packages and stuff!
Any help with this?
sorry but is this more appropriate in the intermediate forum? thanks.
[ September 12, 2003: Message edited by: Melanie Parker ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Think of it this way: when javac needs to find the source for pack1.pack2.Class1, it looks in pack1/pack2/Class1.java relative to the local directory. If it can't find the source, it looks for the class file pack1/pack2/Class1.class ; in this second search, it uses the classpath. It never looks in the current directory; although it knows it's compiling pack1.pack2.Class2 , it does not assume that other classes in this package will be in the same directory. That's just how it is.
Now, let me tell you what I do. This is a little bit of a religious debate, and somebody else may come and say "balderdash!" but you just ignore them
When you run javac, don't run it from inside pack1/pack2. Run it from c:/, and say "javac pack1\pack2\Class2.java" . This works without the classpath setting; you should be able to tell why from the description in the previous paragraph.
Of course, once you've got several classes and packages in a project, you won't want to run javac by hand anymore; you'll want to use a build tool like Ant or make to automate it for you.
This question was just right for the "Basic" forum.


[Jess in Action][AskingGoodQuestions]
Melanie Parker
Greenhorn

Joined: Jun 14, 2003
Posts: 12
Hi,
in this second search, it uses the classpath. It never looks in the current directory

i have no CLASSPATH variable setting, so shouldnt' running javac include the current directory in the classpath automatically?
Is there a resource on the internet that explains how this is actually working? I'm guessing it has to do more with how javac and java find classes other than with packages. I'm reading this in the sdk docs. am i correct.
Thanx...
[ September 12, 2003: Message edited by: Melanie Parker ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Look back at my description above.

If it can't find the source, it looks for the class file pack1/pack2/Class1.class ; in this second search, it uses the classpath.

Is the file "pack1/pack2/Class1.class" available relative to the current directory? Nope. Only Class1.class is. javac won't even look at this file.
As far as a description on the net, this document comes with the J2SDK.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: packages and classpath! or at least i guess!