不明白 javac 对 -classpath 的以下使用
我不太清楚 SCJP 书中的以下问题(尽管我阅读了解决方案和解释)..
考虑以下目录结构:-
foo --> test --> xcom --> A.class, B.java
这里 foo、test 和 xcom 是目录。 A.class和B.java是xcom目录下的文件。
以下是相应文件的源代码:-
A.java
package xcom;
public class A { }
B.java
package xcom;
public class B extends A { }
默认类路径为/foo。
现在,为了编译 B.java,我将当前目录保留为 test 并给出:-
javac -classpath xcom xcom/B.java
这里我给出的类路径为 xcom,其中包含 A.class。但还是没有找到A类。为什么会这样呢?
I am not very clear with the following question from SCJP Book (I read the solution and explanation though) ..
Consider the following directory structure :-
foo --> test --> xcom --> A.class, B.java
Here foo, test and xcom are directories. A.class and B.java are the files in xcom directory.
Following are the source codes of corresponding files:-
A.java
package xcom;
public class A { }
B.java
package xcom;
public class B extends A { }
The default classpath is /foo.
Now, in order to compile B.java, I keep my current directory as test and give :-
javac -classpath xcom xcom/B.java
Here I give the classpath as xcom which has A.class. But still it does not find class A. Why is it so??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的类位于包 xcom 中,那么您的类路径需要位于该包正上方的目录中。在这种情况下,类路径应该是 foo/test。
如果您当前的目录是 foo/test,那么这应该是您的 javac:
If your classes are in package xcom, then your classpath needs to be at the directory directly above that. In this case, the classpath should be foo/test.
And if your current directory is foo/test, then this should be your javac:
因为您必须为
-classpath
参数指定类路径根,例如javac -classpath 。 xcom/B.java
。为了编译B类,java编译器需要A类,它会尝试在{classpathroot}/xcom/
中找到A类文件。注意:
.
- 是当前目录Because you have to specify classpath root to
-classpath
argument, likejavac -classpath . xcom/B.java
. To compile class B java compiler requires class A, it tries to locate class A file in{classpathroot}/xcom/
.Note:
.
- is a current directory我认为这里的根本原因是对Java中“完全限定名称”的误解。
两个类的完全限定名称是 xcom.A 和 xcom.B。它们的源代码位于名为 xcom 的目录中的文件 A.java 和 B.java 中;完全限定名称决定了目录结构。当您要使用这些文件时,无论是编译它们还是运行它们,类路径包含一个或多个可以找到完全限定名称的位置;所以java正在寻找xcom\A.java和xcom\B.java(编译时)以及xcom\A.class和xcom\B.class(运行时)。
这就是为什么类路径需要指定包含 xcom 的目录。
当您进入更复杂的环境时:类路径可以是此类位置的列表;每个位置在 Windows 上用分号分隔,在 UNIX 系统上用冒号分隔。正如您已经看到的,每个位置都可以是一个目录,但它也可以是一个 jar 文件。 jar 文件采用 zip 文件格式,并且 zip 文件具有与磁盘一样的目录结构。因此,您可以压缩类文件,维护它们的 xcom 父级(但不是它们的完整路径),并在类路径而不是目录中指定 jar 文件。
我知道这个问题已经得到了一定的回答,但我想你可能也喜欢背景解释。
I think the root cause here is a misunderstanding of a "fully-qualified name" in Java.
The fully-qualified names of your two classes are xcom.A and xcom.B. Their source is in files A.java and B.java in a directory named xcom; the fully-qualified names dictate the directory structure. When you are going to use the files, either to compile them or run them, the classpath contains one or more locations from which the fully-qualified names can be found; so java is looking for xcom\A.java and xcom\B.java (when compiling) and xcom\A.class and xcom\B.class (when running).
That is why the classpath needs to specify the directory that contains xcom.
As you progress to more complex environments: the classpath can be a list of such locations; each location is separated by a semicolon on windows and a colon on unix systems. Each location can be a directory, as you've already seen, but it can also be a jar file. jar files are in zip file format, and zip files have a directory structure just like disks do. So you could zip up your class files, maintaining their xcom parent (but not their full paths), and specify the jar file in the classpath instead of a directory.
I know the question was already answered somewhat, but thought you might like the background explanation as well.