Java类路径中的点(.)的作用是什么? [英] What is the effect of the dot (.) in the Java classpath?

查看:398
本文介绍了Java类路径中的点(.)的作用是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是"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 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).

我不明白,如果两个文件都在同一个包中,为什么编译器找不到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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆