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 ?
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.