*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Big dilema cap 10 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Big dilema cap 10 "K-B" book" Watch "Big dilema cap 10 "K-B" book" New topic
Author

Big dilema cap 10 "K-B" book

Mirela Muntean
Greenhorn

Joined: Aug 17, 2007
Posts: 13
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

Joined: Feb 26, 2005
Posts: 2049
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

Joined: Dec 17, 2007
Posts: 49
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.


SCJP 6.0 [95%] OCP EJBD 6.0 [93%]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Big dilema cap 10 "K-B" book
 
Similar Threads
Classpath
Need clarification on classpath from SCJP1.6 - Please help
Plz help me in solving the problem of JAVAC comand
I think the answer should be "B" ? K&B SCJP5 errata
Need Clarification on CLASS PATH - Please help