• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

packages and classpath! or at least i guess!

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Melanie Parker
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic