简体   繁体   English

不明白javac使用-classpath之后

[英]Do not understand following use of -classpath by javac

I am not very clear with the following question from SCJP Book (I read the solution and explanation though) .. 我对SCJP书中的以下问题不太清楚(尽管我阅读了解决方案和解释)。

Consider the following directory structure :- 考虑以下目录结构: -

foo --> test --> xcom --> A.class, B.java

Here foo, test and xcom are directories. 这里foo,test和xcom是目录。 A.class and B.java are the files in xcom directory. A.class和B.java是xcom目录中的文件。

Following are the source codes of corresponding files:- 以下是相应文件的源代码: -

A.java A.java

package xcom;
public class A { }

B.java B.java

package xcom;
public class B extends A { }

The default classpath is /foo. 默认的类路径是/ foo。

Now, in order to compile B.java, I keep my current directory as test and give :- 现在,为了编译B.java,我将当前目录作为测试并给出: -

javac -classpath xcom xcom/B.java

Here I give the classpath as xcom which has A.class. 在这里,我将类路径作为xcom提供,其中包含A.class。 But still it does not find class A. Why is it so?? 但它仍然没有找到A级。为什么会这样?

If your classes are in package xcom, then your classpath needs to be at the directory directly above that. 如果您的类在xcom包中,那么您的类路径需要位于该路径正上方的目录中。 In this case, the classpath should be foo/test. 在这种情况下,类路径应该是foo / test。

And if your current directory is foo/test , then this should be your javac: 如果你当前的目录是foo / test ,那么这应该是你的javac:

javac -classpath . xcom/B.java

Because you have to specify classpath root to -classpath argument, like javac -classpath . xcom/B.java 因为必须将classpath root指定为-classpath参数,例如javac -classpath . xcom/B.java javac -classpath . xcom/B.java . javac -classpath . xcom/B.java To compile class B java compiler requires class A, it tries to locate class A file in {classpathroot}/xcom/ . 要编译B类java编译器需要A类,它会尝试在{classpathroot}/xcom/找到A类文件。

Note: . 注意: . - is a current directory - 是当前目录

I think the root cause here is a misunderstanding of a "fully-qualified name" in Java. 我认为这里的根本原因是对Java中“完全限定名称”的误解。

The fully-qualified names of your two classes are xcom.A and xcom.B. 两个类的完全限定名称是xcom.A和xcom.B. Their source is in files A.java and B.java in a directory named xcom; 它们的源代码位于名为xcom的目录中的文件A.java和B.java中; 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). 所以java正在寻找xcom \\ A.java和xcom \\ B.java(编译时)和xcom \\ A.class和xcom \\ B.class(运行时)。

That is why the classpath needs to specify the directory that contains xcom. 这就是类路径需要指定包含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. 每个位置由Windows上的分号和unix系统上的冒号分隔。 Each location can be a directory, as you've already seen, but it can also be a jar file. 每个位置都可以是一个目录,如您所见,但它也可以是一个jar文件。 jar files are in zip file format, and zip files have a directory structure just like disks do. jar文件是zip文件格式,zip文件的目录结构就像磁盘一样。 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. 因此,您可以压缩类文件,维护其xcom父级(但不是它们的完整路径),并在类路径而不是目录中指定jar文件。

I know the question was already answered somewhat, but thought you might like the background explanation as well. 我知道这个问题已经得到了一些回答,但是您认为您可能也喜欢背景说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM