What is the effect of the dot (.) in the Java classpath?

你。 提交于 2019-12-10 15:55:42

问题


This is an example question from "SCJP mock exam":

Given the default classpath:

/foo

And this directory structure:

foo
  |
 test
    |
   xcom
     |--A.class
     |--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

The answer is C, I don't understand the use of the operator . there. Please explain.

The book says:

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 don't get this, if both files are on the same package, why wouldn't the compiler find A?


回答1:


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.




回答2:


In order for B.java to compile, the compiler first needs to be able to find B.java.

This is why D. and E. are wrong.

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. (...)

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.




回答3:


There is no operator ., the . means current directory. Since class A is in the xcom package and since, with javac, the directory hierarchy mirrors the package hierarchy, you need to have a directory in the class path from which the file xcom/A.class can be found. In your case, that is the test directory, so when you invoke javac in that directory, giving the current directory in the class path, javac will find the class xcom.A from the xcom directory.




回答4:


I got confused too while reading your quotation of the book.

Anyway the compiler will look for the A.class and because it is in the same package as B.java, it will look for xcom/A.class. That means you have to tell the compiler where to find your packages and you do this with -classpath.

In your example your packages (it could be more than one) are in test and this what your a telling the compiler with the dot since you are in test.

In short, the compiler looks for class by prefixing their package name as directory.



来源:https://stackoverflow.com/questions/1897132/what-is-the-effect-of-the-dot-in-the-java-classpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!