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 .即使我们还没有编译
也能成功运行?B.java
,pkg/A.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 .即使我们还没有编译 B.java,pkg/A.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
位于不同的包中(前提是 B
是公共的),只要这两个包存在于 wrk
目录中,您可以从 wrk
目录编译 A.java
.
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.
来自 Oracle 文档,用于<代码>javac :
如果未指定 -sourcepath 选项,还会在用户类路径中搜索源文件.
If the -sourcepath option is not specified, the user class path is also searched for source files.
来自 Oracle 文档,用于 CLASSPATH代码>
类路径的默认值为."
如果你没有设置CLASSPATH
,它会默认为.
.随后,sourcepath
也将是 .
,因为默认的 sourcepath
与 CLASSPATH
相同.您可以通过使用 javac -verbose -g pkgA.java
编译 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 pkgA.java
. Notice that the compiler is looking in the current directory for .java
files :
[解析开始的pkgA.java][解析完成29ms][源文件搜索路径:[.]]
要确认sourcepath
设置为CLASSPATH
,您可以尝试使用-cp
更改CLASSPATH
> 选项通过使用 javac -cp C: -verbose -g pkgA.java
编译 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 pkgA.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 :
[解析开始的pkgA.java]【解析完成26ms】[源文件的搜索路径:[C:]]pkgA.java:3: 找不到符号符号:B类
另外如果当前目录是/top/wrk/pkg 那么命令 javacA.java 工作.怎么样?
Also if the current directory is /top/wrk/pkg then the command javac A.java works. How so?
无论 B.class
是否存在于 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屋!