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

This is a sample question from the "SCJP mock exam":

Given the default default path:

/ 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 { } 

What allows B.java compilation? (Select all that apply.)

a. Set the current directory to xcom , then call

 javac B.java 

B. Install the current directory on xcom , then call

 javac -classpath . B.java 

C. Set the current directory to check, then call

 javac -classpath . xcom/B.java 

D. Set the current directory to check, then call

 javac -classpath xcom B.java 

E. Set the current directory to check, then call

 javac -classpath xcom:. B.java 

Answer: C, I do not understand the use of the operator . . Please explain.

The book says:

To compile B.java compiler must first find B.java . Having found B.java , he should find A.class . Because A.class is in the xcom package, the compiler will not find A.class if it is called from the xcom directory. Remember that -classpath not looking for B.java , it is looking for any B.java classes (in this case A.class ).

I do not understand if both files are in the same package, why the compiler will not find A?

+4
source share
4 answers

dot means "current directory". If you call javac from xcom , it will look for A.class in xcom/xcom/A.class and will not find it.

+4
source

To compile B.java compiler must first find B.java .

That is why D. and E. are mistaken.

Once he finds B.java , he needs to find A.class . Since A.class is in the xcom package, the compiler will not find A.class if it is called from the xcom directory. (...)

You missed the important part here, which , if called from the xcom directory . A.class is in the xcom package, so it can be found in xcom/A.class (regarding where you run javac).

That is why A. and B. are wrong. And that leaves S. as the correct answer.

+3
source

No operator . , . means current directory. Since class A is in the xcom package, and since with javac directory hierarchy reflects the package hierarchy, you need to have a directory in the class path from which the xcom/A.class file can be found. In your case, this is the test directory, so when you call javac in this directory, specifying the current directory in the class path, javac will find the xcom.A class from the xcom directory.

+1
source

I, too, was embarrassed reading your quote from a book.

In any case, the compiler will look for A.class and because it is in the same package as B.java , it will look for xcom/A.class . This means that you must tell the compiler where to find your packages, and you do this with -classpath .

In your example, your packages (there may be several) are in test , and this is what you tell the compiler with a dot, since you are in the test.

In short, the compiler is looking for a class, the prefix of its package name as a directory.

0
source

All Articles