<tfoot id='OTlCV'></tfoot>

<i id='OTlCV'><tr id='OTlCV'><dt id='OTlCV'><q id='OTlCV'><span id='OTlCV'><b id='OTlCV'><form id='OTlCV'><ins id='OTlCV'></ins><ul id='OTlCV'></ul><sub id='OTlCV'></sub></form><legend id='OTlCV'></legend><bdo id='OTlCV'><pre id='OTlCV'><center id='OTlCV'></center></pre></bdo></b><th id='OTlCV'></th></span></q></dt></tr></i><div id='OTlCV'><tfoot id='OTlCV'></tfoot><dl id='OTlCV'><fieldset id='OTlCV'></fieldset></dl></div>
      • <bdo id='OTlCV'></bdo><ul id='OTlCV'></ul>

        <legend id='OTlCV'><style id='OTlCV'><dir id='OTlCV'><q id='OTlCV'></q></dir></style></legend>
      1. <small id='OTlCV'></small><noframes id='OTlCV'>

        javac如何自动编译一个类的依赖

        时间:2023-09-27
          <tbody id='lctAQ'></tbody>

        <tfoot id='lctAQ'></tfoot>
      2. <small id='lctAQ'></small><noframes id='lctAQ'>

        1. <legend id='lctAQ'><style id='lctAQ'><dir id='lctAQ'><q id='lctAQ'></q></dir></style></legend>

              <bdo id='lctAQ'></bdo><ul id='lctAQ'></ul>
            • <i id='lctAQ'><tr id='lctAQ'><dt id='lctAQ'><q id='lctAQ'><span id='lctAQ'><b id='lctAQ'><form id='lctAQ'><ins id='lctAQ'></ins><ul id='lctAQ'></ul><sub id='lctAQ'></sub></form><legend id='lctAQ'></legend><bdo id='lctAQ'><pre id='lctAQ'><center id='lctAQ'></center></pre></bdo></b><th id='lctAQ'></th></span></q></dt></tr></i><div id='lctAQ'><tfoot id='lctAQ'></tfoot><dl id='lctAQ'><fieldset id='lctAQ'></fieldset></dl></div>
                • 本文介绍了javac如何自动编译一个类的依赖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  给定以下目录结构:

                  /top
                     |--- wrk
                            |--- pkg
                                    |--- A.java
                                    |--- B.java
                  

                  假设A.javaB.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.javaB.java 在同一个包中.即使 B.javaA.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 也将是 .,因为默认的 sourcepathCLASSPATH 相同.您可以通过使用 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,您可以尝试使用 -cpCLASSPATH> 使用 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如何自动编译一个类的依赖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Eclipse - 如何“将包声明更改为 ...."在整个项 下一篇:Java 包导入别名

                  相关文章

                  最新文章

                  • <bdo id='uU4eE'></bdo><ul id='uU4eE'></ul>
                • <i id='uU4eE'><tr id='uU4eE'><dt id='uU4eE'><q id='uU4eE'><span id='uU4eE'><b id='uU4eE'><form id='uU4eE'><ins id='uU4eE'></ins><ul id='uU4eE'></ul><sub id='uU4eE'></sub></form><legend id='uU4eE'></legend><bdo id='uU4eE'><pre id='uU4eE'><center id='uU4eE'></center></pre></bdo></b><th id='uU4eE'></th></span></q></dt></tr></i><div id='uU4eE'><tfoot id='uU4eE'></tfoot><dl id='uU4eE'><fieldset id='uU4eE'></fieldset></dl></div>

                • <tfoot id='uU4eE'></tfoot>

                  <small id='uU4eE'></small><noframes id='uU4eE'>

                  1. <legend id='uU4eE'><style id='uU4eE'><dir id='uU4eE'><q id='uU4eE'></q></dir></style></legend>