Basically, the package xcom specifies where the physical location of the file is. The physical location of A is xcom\A.class
A. Set the current directory to xcom then invoke
javac B.java
Explanation : B uses A, but this command does not specify any classpath to find A.
B. Set the current directory to xcom then invoke
javac -classpath . B.java
Explanation: -cp . means looking at xcom folder, find A. It seems to work. But A's full qualified name is xcom.A
Going to xcom folder find xcom.A However, under xcom, there is not another xcom folder. There is no such a folder xcom\xcom
So, javac cannot find A.
But javac can find B because B is in xcom and javac is used in xcom.
This is the most confusing choice.
C. Set the current directory to test then invoke
javac -classpath . xcom/B.java
Explanation: Go to the current directory test, javac will find xcom folder. Under test directory, javac will find xcom/B.java
It works.
D. Set the current directory to test then invoke
javac -classpath xcom B.java
Explanation: Under test directory, javac will look at xcom. However, there is no other xcom folder under xcom. There is no such a directory xcom/xcom.
Therefore, javac cannot find A. Javac cannot find B neither. Under test directory, there is no such a B.java file. B.java is under xcom.
E. Set the current directory to test then invoke
javac -classpath xcom:. B.java
Explanation: Javac will look at xcom, but cannot find xcom.A . Then, it looks at the current directory, test. It finds xcom.A !
However, javac cannot find B.java because B is under xcom, not under test.
This is the most confusing topic for me, too. This is even more confusing that generics/threads topics, honestly. It is because people nowadays use Eclipse/JBuilder or other tools to compile. We don't use command line to compile ourselves anymore.
Summary:
For javac, -cp looks for all the files that are needed to compile a file. If the files , such as xcom.A is needed, it should look at test directory in order to find xcom. Getting into xcom to find xcom.A means looking for a file xcom\xcom\A.class. There is no such file path.
For javac, to compile the file , such as B.java, javac has to know where B.java is. If it is in the same current directory , fine, just use B.java. If it is in deeper directories, then specify it. Or, if it is in your thumb drive F: , then specify F:\B.java
For
java, -cp actually looks for all files , not just A.class, but also B.class . This is because it needs to make sure A.class and B.class still exists. However, in javac, the -cp only looks for the classes (A.class) it needs to compile B.java. This is a big difference between java and javac in the way how they use -cp.
If the current directory is test, the command should be java -cp . xcom.B (it means, java looks at the current directory test, look for xcom.A and xcom.B, and execute xcom.B)
If the current directory is xcom, I don't think the command will work for B. Because B's full name is xcom.B . Under xcom, there is no such a xcom\B.
Correct me if I am wrong. I am still a learner about this confusing topic.