javac如何自动编译类的依赖关系 [英] How does javac automatically compile dependencies of a class
问题描述
鉴于以下目录结构:
/top
|--- wrk
|--- pkg
|--- A.java
|--- B.java
假设两个文件 A.java
和 B.java
分别包含以下代码:
Assume that the two files A.java
and B.java
contain the following code, respectively:
// Filename: A.java
package pkg;
class A { B b; }
// Filename: B.java
package pkg;
class B {...}
假设当前目录是 / top / wrk
为什么命令 javac -cp。 pkg / A.java
成功工作,即使我们还没有编译 B.java
?
Why does the command javac -cp . pkg/A.java
work successfully even though we have not yet compiled B.java
?
此外,如果当前目录是 / top / wrk / pkg
,则命令 javac A.java
有效。怎么会这样?
Also if the current directory is /top/wrk/pkg
then the command javac A.java
works. How so?
推荐答案
为什么命令javac -cp。 pkg / A.java工作成功,即使我们还没有编译B.java
Why does the command javac -cp . pkg/A.java work successfully even though we have not yet compiled B.java
当你编译 A .java
,编译器将编译 B.java
以及 A.java
和 B.java
在同一个包中。即使 B.java
与 A.java
不同,也可以使用此功能(提供 wrk
目录中并且您编译 A.java 是公共的) / code>来自
wrk
目录。
When you compile A.java
, the compiler will compile B.java
as well since both A.java
and B.java
are in the same package. This will work even If B.java
was in a different package from A.java
(provided B
is public) as long as both the packages are present in the wrk
directory and you compile A.java
from wrk
directory.
来自 javac 的
如果未指定-sourcepath选项,则还会在用户类路径中搜索源文件。
If the -sourcepath option is not specified, the user class path is also searched for source files.
来自 Oracle文档 CLASSPATH
类路径的默认值是。
The default value of the class path is "."
如果你还没有设置 CLASSPATH
,它将会默认为。
。随后, sourcepath
也将是。
,因为默认的 sourcepath
与 CLASSPATH
相同。您可以通过使用 javac编译
。请注意,编译器在当前目录中查找 A.java
来确认默认源路径设置为。
-verbose -g pkg \A.java .java
文件:
If you haven't set a CLASSPATH
, it will be defaulted to .
. Subsequently, the sourcepath
will also be .
since the default sourcepath
is the same as the CLASSPATH
. You can confirm that the default sourcepath is set to .
by compiling A.java
using javac -verbose -g pkg\A.java
. Notice that the compiler is looking in the current directory for .java
files :
[解析开始pkg\A.java]
[解析完成29ms]
[源文件的搜索路径:[。]]
要确认 sourcepath
设置为 CLASSPATH
,您可以尝试更改 CLASSPATH
使用 -cp
选项,使用<$ c编译 A.java
$ c> javac -cp C:\ -verbose -g pkg \A.java 。 A.java
这次不会编译,因为你已经将 CLASSPATH
覆盖到 C:\\ \\
,这也是默认的 sourcepath
。这是输出:
To confirm that the sourcepath
is set to CLASSPATH
, you can try changing the CLASSPATH
using the -cp
option by compiling A.java
using javac -cp C:\ -verbose -g pkg\A.java
. A.java
will not compile this time since you have overwritten the CLASSPATH
to C:\
and that's what sourcepath
will default to as well. This is the output :
[解析已启动pkg \A.java]
[解析完成26ms]
[源文件的搜索路径:[C:\]]
pkg \A.java:3:找不到符号
符号:B类
此外,如果当前目录是/ top / wrk / pkg,那么命令javac
A.java也可以。怎么样?
Also if the current directory is /top/wrk/pkg then the command javac A.java works. How so?
无论 B.class
是否为零,这都行不通出现在 pkg
This will not work regardless of whether B.class
is present in pkg
免责声明:我只能在Windows上确认此行为,但我非常怀疑它在其他操作系统上应该有什么不同。
Disclaimer : I can only confirm this behavior on Windows but I highly doubt that it should be any different on other operating systems.
这篇关于javac如何自动编译类的依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!