You cannot use classes in a default package from a named package.
(Technically, you can, as shown in Sharique Abdullah, respond via the reflection API, but classes from the nameless namespace are not within the scope of the import declaration )
Prior to J2SE 1.4, you could import classes from the default package using this syntax:
import Unfinished;
This is no longer permitted . Therefore, to access the default package class from the packaged class, you must move the default package class to your own package.
If you have access to the source generated by groovy, some post-processing is required to move the file to the selected package and add this “package” directive at the beginning.
Update 2014: bug 6975015 , for JDK7 and JDK8, describes an even stricter ban on importing from an unnamed package.
TypeName must be the canonical name of the class type, interface type, enumeration type, or annotation type.
A type must be either a member of a named package , or a member of a type whose outer lexically spanning type is a member of a named package , or a compile-time error occurs .
Andreas points out in the comments :
"why does [default package] exist in the first place? design error?"
No, this is intentional.
JLS 7.4.2. Unnamed packages state: "Unnamed packages are provided by the Java SE platform mainly for convenience when developing small or temporary applications or just at the beginning of development."
VonC Nov 12 '08 at 12:44
source share