Java类路径中的点(.)的作用是什么? [英] What is the effect of the dot (.) in the Java classpath?
问题描述
这是"SCJP模拟考试"中的示例问题:
This is an example question from "SCJP mock exam":
给出默认的类路径:
Given the default classpath:
/foo
这个目录结构:
foo
|
test
|
xcom
|--A.class
|--B.java
这两个文件:
package xcom;
public class A { }
package xcom;
public class B extends A { }
哪个允许B.java进行编译? (选择所有适用条件.)
Which allows B.java to compile? (Choose all that apply.)
A.将当前目录设置为xcom
然后调用
A. Set the current directory to xcom
then invoke
javac B.java
B.将当前目录设置为xcom
然后调用
B. Set the current directory to xcom
then invoke
javac -classpath . B.java
C.将当前目录设置为测试然后调用
C. Set the current directory to test then invoke
javac -classpath . xcom/B.java
D.将当前目录设置为测试然后调用
D. Set the current directory to test then invoke
javac -classpath xcom B.java
E.将当前目录设置为测试然后调用
E. Set the current directory to test then invoke
javac -classpath xcom:. B.java
答案是C,我不知道该运算符.
的使用.请解释.
The answer is C, I don't understand the use of the operator .
there. Please explain.
这本书说:
为了编译
B.java
,首先需要编译器能够找到B.java
. 一旦找到B.java
,就需要找到A.class
. 因为A.class
在 如果xcom
包是从xcom
目录中调用的,则编译器将找不到A.class
. 请记住,-classpath
不是在寻找B.java
,而是在寻找任何类B.java
需求(在本例中为A.class
).
In order for
B.java
to compile, the compiler first needs to be able to findB.java
. Once it's foundB.java
, it needs to findA.class
. BecauseA.class
is in thexcom
package the compiler won't findA.class
if it's invoked from thexcom
directory. Remember that the-classpath
isn't looking forB.java
, it's looking for whatever classesB.java
needs (in this caseA.class
).
我不明白,如果两个文件都在同一个包中,为什么编译器找不到A?
I don't get this, if both files are on the same package, why wouldn't the compiler find A?
推荐答案
点表示当前目录".
如果从xcom
内部调用javac,则它将在xcom/xcom/A.class
中查找A.class
,并且找不到它.
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.
这篇关于Java类路径中的点(.)的作用是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!