gpt4 book ai didi

java - 分包进口按需申报

转载 作者:搜寻专家 更新时间:2023-11-01 03:24:38 27 4
gpt4 key购买 nike

相关:How can I compile "import pack.*" with ant/javac, when there are no such classes?

假设我们有给定的包结构

parent
|
---a
---b

parent包只包含ab两个子包(parent包下没有类>).

代码 import parent.*,位于 parent 以外的包中,使用 Maven(即 javac)编译,抛出一个编译 -时间错误。错误是:

package parent does not exist

我查看了关于这种情况的 Java 语言规范(按需导入声明,实际上没有导入任何类型)。 Paragraph 7.5.2 of the JLS关于按需导入声明似乎没有指定此行为。

这是 javac 的错误吗?我不是在问如何规避编译错误。我想知道为什么 javac 会抛出错误,因为我在 JLS 中找不到它应该找到的任何引用;让我相信这可能是一个错误。

我用JDK 1.4、1.6、1.7和1.8测试,错误都是一样的。

作为旁注,在 Eclipse 中使用相同的代码没有编译错误(使用 Eclipse Indigo、Juno、Luna 和 Mars 进行测试)。

注意:我在对仅依赖于 Eclipse 编译器的(旧)现有项目进行“mavenizing”时遇到了这种行为。我花了一段时间才确定这是我遇到的编译错误的根本原因。

最佳答案

我想你要找的是section7.4.3

A package is observable if and only if either:

  • A compilation unit containing a declaration of the package is observable (§7.3).

  • A subpackage of the package is observable.

The packages java, java.lang, and java.io are always observable.

并且在 7.5.2

It is a compile-time error if the named package or type is not accessible (§6.6).

因此,如果包中没有任何内容,那么它就不存在,而错误实际上出在 Eclipse 编译器方面;在 very old bug report 中有描述它一直处于“稍后”状态,直到最终该状态被弃用并改为 WONT_FIX。如果您认为它值得修复,请随时重新打开它(现在 JLS 在这方面得到了澄清)

关于java - 分包进口按需申报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18004757/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com