Community
Participate
Working Groups
According to the Java language spec a CompilationUnit is allowed to be empty: CompilationUnit: [package QualifiedIdentifier ; ] {ImportDeclaration} {TypeDeclaration} This means the following is a valid Java class, regardless of which package it is in: -- START-- // just a single comment -- END-- However, Eclipse compiler generates "The declared package does not match the expected package ..." error. This is not an issue with javac. Dev teams in my company use empty class skeletons like the one above when adding them to the source code repository before checking-in workable versions [that is, an empty file is added first to reserve the name in the package namespace and then a "real" version is added later]. Eclipse is the only IDE that fails to work with empty files. Apparently, there are not preference settings to disable this behavior.
Eclipse is performing an extra check, so as to ensure that the source file is located in the right directory structure matching its package declaration. javac doesn't care, at least if you compile these file by adding them onto the command line. If you only put these sources on its classpath, then it will require them to be properly laid out, so as to infer their location based on their qualified name. We relax the check for completely empty units (if you remove the comment, you'll notice we don't complain any longer). We thought this would be enough to accomodate people annoyed by the extra check, and you seem to have a use case where this is not flexible enough.
Changed the implementation to only check for package expectation if the unit contains any structure (package, import, type). Comments alone are no longer sufficient to cause some reporting. Fixed.
Also see bug 22418 for original same request for strictly empty files.
Verified.