问题描述
相关:我如何编译import pack.*"用ant/javac,什么时候没有这样的类?
假设我们有给定的包结构
Suppose we have the given package structure
parent
|
---a
---b
其中包parent
只包含两个子包a
和b
(包parent
下没有类>).
where the package parent
only contains the two subpackges a
and b
(no class is under the package parent
).
代码import parent.*
,位于除parent
之外的包中,使用Maven(即javac
)编译时抛出编译-时间错误.错误是:
The code import parent.*
, situated in a package other than parent
, compiled with Maven (i.e. javac
) throws a compile-time error. The error is:
包父级不存在
我查看了有关这种情况的 Java 语言规范(实际上没有导入类型的按需导入声明).第 7.5.2 段JLS 关于按需导入声明似乎没有指定这种行为.
I looked into the Java Language Specification about such a case (import-on-demand declaration where no types are actually imported). Paragraph 7.5.2 of the JLS about import-on-demand declaration does not seem to specify this behavior.
这是javac
的bug吗?我不是在问如何规避编译错误.我想知道为什么 javac
会抛出错误,因为我在 JLS 中找不到它应该的任何引用;让我相信这可能是一个错误.
Is this a bug of javac
? I am not asking how to circumvent the compile error. I would like to know why javac
throws an error because I cannot find any reference in the JLS that it should; leading me to believe that this might be a bug.
我用 JDK 1.4、1.6、1.7 和 1.8 测试过,错误是一样的.
I tested with JDK 1.4, 1.6, 1.7 and 1.8, the error is the same.
顺便说一下,在 Eclipse 中使用相同的代码没有编译错误(使用 Eclipse Indigo、Juno、Luna 和 Mars 测试).
As a side note, there is no compile error with the same code in Eclipse (tested with Eclipse Indigo, Juno, Luna and Mars).
注意:我在mavenizing"一个仅依赖于 Eclipse 编译器的(旧)现有项目时遇到了这种行为.我花了一段时间才确定这是我遇到的编译错误的根本原因.
NB: I encountered this behaviour when "mavenizing" an (old) existing project that only relied on the Eclipse compiler. It took me a while to identify that this was the root cause of the compile error I was getting.
推荐答案
我认为您正在寻找的是 section7.4.3
I think what you are looking for is section7.4.3
一个包是可观察的,当且仅当:
包含包声明的编译单元是可观察的(第 7.3 节).
A compilation unit containing a declaration of the package is observable (§7.3).
包的子包是可观察的.
包 java、java.lang 和 java.io 始终是可观察的.
The packages java, java.lang, and java.io are always observable.
在 7.5 中.2
如果指定的包或类型不可访问(第 6.6 节),则会导致编译时错误.
因此,如果包中没有任何内容,则它不存在并且错误实际上在 Eclipse 编译器一侧;它在 非常老的错误报告 中有所描述,该报告留在了LATER"中状态,直到最终该状态被弃用,并将其移至 WONT_FIX.如果您认为值得修复,请随时重新打开它(现在 JLS 在这方面已得到澄清)
So if nothing is in a package it does not exists and the bug is actually on the side of the Eclipse compiler; it's described in a very old bug report that was left in "LATER" state until eventually that state was deprecated and it was moved to WONT_FIX instead. Feel free to re-open it if you think it's worth fixing (now that the JLS is clarified in this area)
这篇关于仅包含子包的按需导入声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!