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

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

问题描述

这是来自SCJP模拟考试"的示例问题:

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

给定默认类路径:

/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.将当前目录设置为 test 然后调用

C. Set the current directory to test then invoke

javac -classpath . xcom/B.java

D.将当前目录设置为 test 然后调用

D. Set the current directory to test then invoke

javac -classpath xcom B.java

E.将当前目录设置为 test 然后调用

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.classxcom 包编译器将找不到 A.class 如果它是从 xcom 目录调用的.请记住,-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天全站免登陆