• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

K&B package question

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everybody

I have a doubt in packages chapter in K&B pg 783 Q5.

The package structure is like this

foo
|
test
|
xcom
|
A.class
B.java

In order to compile B.java i think the answer in option B
ie set current directory to xcom and run javac -classpath . B.java
because the class file A.class is in current directory.
But the correct answer is option C which says
set current directory to test and run javac -classpath . xcom/B.java
I understand the part xcom/B.java but I dont understand how the compiler finds A.class in current directory ie in test.
I wrote the program and when i run option C i am able to compile it.Im sure I am wrong.What am i missing here?
Thanks in advance.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Was A.class declared to be in package xcom? If so, then its qualified name is xcom.A.class, and this is found in the directory test.
[ July 31, 2007: Message edited by: marc weber ]
 
praneeth kumar
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply.

I removed package statement from A.Now there is no package statement in A.java, and A.java and A.class are in xcom.But now when I run
javac -classpath . B.java from xcom, still i get error like A.class not found.

What is the error now???
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But B is still in the package xcom, right? And A is now in the default package that has no name. So how can the package with no name be imported into B? (See this related thread in which I posted, "I don't see that the unnamed package is automatically imported if the current package is named.")

So if you remove the package declaration from B, this should work. But since this exercise seems to be about using packages, that would also defeat the purpose. So I think you should leave both classes in the package xcom, and then compile as instructed (that is, set the current directory to test and run javac -classpath . xcom/B.java).
 
Hey! You're stepping on my hand! Help me tiny ad!
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic