This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Big dilema cap 10 "K-B" book

 
Mirela Muntean
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

I really need some help because I will take the exam this week, and I don't understand the answer to the Q5 at the end of cap 10.

Question is: Given the structure

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

And the classes:
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

In the book the correct answer is C, and the explanation is:
In order for B.java to compile, the compiler first needs to be able to find B.java. Once it's found B.java it needs to find A.class. Because A.class is in the xcom package the compiler won't find A.class if it's invoked from the xcom directory. Remember that the -classpath isn't looking for B.java, it's looking for whatever classes B.java needs (in this case A.class).

I chosed the B version, because in the book it sais that javac looks the .java files implicitly in the current directory. So, you set the current directory to xcom and javac will find the A.class, and the B.java will also be found because it is a .java file and will be searched implicitly in the current directory.

In the book, pg 768 it sais: "Remember .... when you're telling javac
which .java file to compile, javac looks in the current directory by default.
"

Can anyone clarify why the javac will not find B.java if the current directory is "xcom" , and why the C version is the correct one ?

Thank you
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The classes belonged to package xcom.

Therefore, the javac will search, based on the classpath, for a class /xcom/B.java. If current location is 'test', then javac will successfully find test/xcom/B.java.

If the current location is xcom, then the javac will look for a /xcom/B.java 'inside' the xcom folder - which is wrong (That would imply a folder structure like /xcom/xcom/B.java.).
 
Serg Masow
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mirela,

if you set the current directory to xcom an invoke the compiler like this
javac -classpath . B.java

javac will find the sourcefile B.java, but for the successful compilation it also needs the A.class file. And here is a problem, because the class A is in the xcom package compiler try to look into subdirectory xcom of the current directory. Since we are already in the directory xcom and there is no subdirectory xcom compilation fails.

Hope this helps to understand the problem.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic