Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

Classpath doubt k&b chp 10 que 5  RSS feed

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Given the default class path /foo
and the following dir structure
foo
test
xcom > A.class and B.java in xcom

And these two files

package xcom;
public class A{}

package xcom;
public class B extends A{}

Which allows B.java to compile?

A.Set the current directory to xcom and invoke
javac B.java

B.Set the current directory to xcom and invoke
javac -classpath . B.java

C.Set the current directory to test and invoke
javac -classpath . xcom/B.java

D.Set the current directory to test and invoke
javac -classpath xcom B.java

E.Set the current directory to test and invoke
javac -classpath xcom: . B.java


Can any1 tell me 1 by 1 why does B,D and E fail?
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Asif!

Assumption:
default classpath is /foo
classes A and B are in /foo/test/xcom


ad a) javac B.java
Current directory: /foo/test/xcom
The compiler needs class A but cannot find it: the compiler looks if directory test has a subdirectory xcom. Since there is no such directory compilation fails. If you'd change the classpath to foo/test it would work (see also answer c).

ad b) javac -classpath . B.java
Current directory: /foo/test/xcom
You override the default classpath and set it to the current directory. Again, there is no subdirectory xcom with class A in it and therefore compilation fails.

ad c) javac -classpath . xcom/B.java
Current directory: /foo/test
That's ok:
javac xcom/B.java is the correct path to class B (and we wouldn't need the classpath setting if B wouldn't extend A).
In order to tell the compiler the location of A we override the default classpath and set it to foo/test: that's ok, because now the compiler finds a subdirectory xcom with class A in it!

ad d)javac -classpath xcom B.java
Current directory: /foo/test
The basic problem: we don't tell the compiler the correct path to class B, thus it cannot find it. Note that telling the correct path to the class to be compiled is not done via the classpath setting but always directly, i.e. either you are in the directory of the class or you type in the whole path (e.g. as in answer c)

ad e)javac -classpath xcom:. B.java
Current directory: /foo/test
Also here: the compiler cannot find class B (see answer d).

Cheers,
Robert
 
Asif Garhi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert nice explainations,thanks.
One more doubt though.
why in a and b is xcom being searched?
Is it bcoz we have specified package xcom at the top of both the classes.
As far as i know this happens when you use the -d option with javac.
[ July 25, 2007: Message edited by: Asif Garhi ]
 
Robert Horvath
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

You're absolutely right: the compiler searches within all classpaths the directory xcom because of the package declaration.

But if you'd remove package xcom; in both class A and B, there would still be a problem: Since the default classpath is /foo the compiler would now try to find class A in the directory foo! NO package declaration just means, that the compiler tries to find the class directly in one of his classpaths (instead of a subdirectory).

If you'd like to remove the package declaration anyway and assuming that the current directory is /foo/test/xcom and the default classpath is /foo, then a valid solution to compile class B would be:
javac -classpath . B.java

Kind regards,
Robert
 
Robert Horvath
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Asif,

I forgot the -d issue ;-)

-d ("directory") just tells the compiler where to put the class-files after compilation. E.g., javac -d myoutputdirectory B.java tells the compiler to put B.class into a subdirectory called "myoutputdirectory". -d is completely independent from the package declaration stuff and the things we talked before!

Cheers,
Robert
 
Asif Garhi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Robert,
Hope to be doing classpath questions dead right in the exam.
Might appear on the 30th of this month.
Pray for me friend.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!