This is an example question from "SCJP mock exam":
Given the default classpath:
/foo
And this directory structure:
foo | test | xcom |--A.class |--B.javaAnd 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
xcomthen invokejavac B.javaB. Set the current directory to
xcomthen invokejavac -classpath . B.javaC. Set the current directory to test then invoke
javac -classpath . xcom/B.javaD. Set the current directory to test then invoke
javac -classpath xcom B.javaE. Set the current directory to test then invoke
javac -classpath xcom:. B.java
The answer is C, I don't understand the use of the operator . there. Please explain.
The book says:
In order for
B.javato compile, the compiler first needs to be able to findB.java. Once it's foundB.java, it needs to findA.class. BecauseA.classis in thexcompackage the compiler won't findA.classif it's invoked from thexcomdirectory. Remember that the-classpathisn't looking forB.java, it's looking for whatever classesB.javaneeds (in this caseA.class).
I don't get this, if both files are on the same package, why wouldn't the compiler find A?
the dot means 'the current directory'.
If you call javac from within xcom, then it will look for A.class in xcom/xcom/A.class, and won't find it.
In order for
B.javato compile, the compiler first needs to be able to findB.java.
This is why D. and E. are wrong.
Once it's found
B.java, it needs to findA.class. Because A.class is in thexcompackage the compiler won't findA.classif it's invoked from thexcomdirectory. (...)
You missed the important part here which is if it's invoked from the xcom directory. A.class is in the xcom package so it is expected to be found in xcom/A.class (relatively to where you run javac).
This is why A. and B. are wrong. And this leave C. as the right answer.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With