• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question about classpath from K&B book  RSS feed

 
Sawan Mishra
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm getting another doubt.this is frm k&B book

chapter 10 question 3
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

Answer:C

Explanation in book:
classpath isn't looking for B.java its looking for whatever classes B.java needs

I don't understand why A> and B> are wrong..
In xcom directory if we are giving command "javac B.java" then it should compile since B.java and A.class both are present in that directory.But it doesn't happen so.
If B.java doesn't need other class files then A> and B> works f9 but if it needs class files as of here it needs A.class so it doesn't work...
I can't understand properly.
please explain in detail...

thanks
with regards
sawan
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While A and B are in that directory, the first two choices would only work if you removed the package statements. "package xcom" is a statement that Java expects them to be in an xcom directory underneath the working directory (or in a jar file or on the classpath.)
 
Sawan Mishra
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Boyarsky
But if there is A.java file present in the same directory and if I compile then A.java with the command "javac A.java",it compiles but "javac B.java" doesn't.
As B extends from another class file present in the same package it doesn't compile. why??

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!