Hey,
I think I understand why Sierra/Bates, Chapter 10, Question 3 is C, and the other options are incorrect. Will appreciate if you critique my understanding of this subject, please see below quote:
Given the default classpath:
/foo
And this directory structure:
foo / test / xcom / A.class
foo / test / xcom / 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
A: B.java depends on A.class to compile. Since A.class is part of xcom package, you need to be one directory above xcom because compiler won't see A.class
B. Similar explanation for why option A is incorrect. Setting the classpath to current directory and xcom makes to difference because A.class is part of xcom package
C. This is CORRECT. Move up to
test directory, set the classpath to current directory by typing in "." and then type the relative path of the program to compile, i.e, xcom/B.java
D. Even though you are in test directory and set classpath, it won't work because xcom is a relative path, but relative to what? There can be numerous directories named xcom throughout your filesystem
E. Ok, at least the syntax for setting classpath is correct, but it will first look at xcom, and then "." which is the current directory. This would be correct if you attempt to compile "xcom/B.java", because B is part of xcom, rather than B.java, because compiler won't understand what B.java is