• 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

confused in classpath related question

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the default classpath:
/foo
And this directory structure:
foo
|
test
|
xcom
|--A.class
|--B.java

And these two files:


package xcom;
public class A { }

package xcom;
public class B extends A { }

Which allows B.java to compile? (Choose all that apply.)

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

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

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

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

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

why option b is not correct
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Dharmendra Ramanujan wrote:Given the default classpath:
/foo
And this directory structure:
foo
|
test
|
xcom
|--A.class
|--B.java

And these two files:


package xcom;
public class A { }

package xcom;
public class B extends A { }

Which allows B.java to compile? (Choose all that apply.)

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

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

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

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

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

why option b is not correct




Hello Dharmendra,
The following may help you,
The root of xcom package is the "test" directory. So the javac must be executed from that directory only .
In simpler words, the classes A and B are in package xcom (they are put in a box xcom) so we can see only xcom.A and xcom.B classes from test directory.

So your case, option b is incorrect because:-





The compiler sees it as

foo
|
test
|-xcom.A
|-xcom.B


When you are in directory xcom, you need a "A.class" to compile B , but that class exists in xcom package so we need a xcom.A class but that is not in current directory (i.e. -> "." )(xcom/xcom/A.class is not valid) , but it is present in previous directory (test)(as test/xcom/A.class is valid) so we can modify this command to take xcom.A (from test directory) as follwing:-







The correct options are only C and E because in C current directory has xcom.A (or xcom/A) and same is in E too.
In option D current directory is not included and xcom does not contain xcom.A (as it searches for xcom/xcom/A)


Reply if it helped.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That question is from K&B (as cited here. In the future, please post where you got a question from when posting it. Thanks.
 
reply
    Bookmark Topic Watch Topic
  • New Topic