Question

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

Suppose we have the given package structure

parent
|
---a
---b

where the package parent only contains the two subpackges a and b (no class is under the package parent).

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:

package parent does not exist

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.

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.

I tested with JDK 1.4, 1.6, 1.7 and 1.8, the error is the same.

As a side note, there is no compile error with the same code in Eclipse (tested with Eclipse Indigo, Juno, Luna and Mars).

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.

Was it helpful?

Solution

I think what you are looking for is 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.

And in 7.5.2

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

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)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top