I'm trying to add package level annotations but I don't have a clue on how to do it. Examples are appreciated.
4 Answers
In eclipse
Since package-info.java isn't a valid identifier for a class it cannot be created as a class in Eclipse.
I found that when you create a new package there is a check box to check if you want a package-info.java.
To create a package-info.java file in an existing package:
- Right-click on the package where you want a package-info.java.
- Select New->Package.
- Check the Create package-info.java check box.
- Click on Finish.

- 40,330
- 4
- 86
- 117

- 6,939
- 6
- 35
- 38
-
2This does not work in my Eclipse version (3.7.2) - which version is required for this solution? – Michael Schmeißer Apr 22 '13 at 14:06
-
I have Juno, version 4.2.0. – javabeangrinder Apr 22 '13 at 16:00
-
2To add such a file to an existing package, I right-click on the package in the Project Explorer → New → Other → File and manually name it package-info.java. I keep forgetting exactly what to call the file and which annotations to add, though, so I'd love to read of a more elegant way. – Michael Scheper Jun 21 '13 at 07:47
Summary from the article here
In package-info.java:
@PackageLevelAnnotation
package blammy; // package with a package level annotation.
import blammy.annotation.PackageLevelAnnotation;
In PackageLevelAnnotation.java
package blammy.annotation;
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.PACKAGE)
public @interface PackageLevelAnnotation
{
// stuff as required.
}
Edit: more package level info. Here is a link to the package chapter in the Java Language Spec: packages

- 37,124
- 11
- 56
- 82
-
1Where is package-info.java? Is it provided or somewhere or do I have to create it? Where would I need to create this file if so. Thanks. – link_boy Dec 06 '11 at 19:27
-
In my example above, the file package-info.java should be in the blammy package (i.e. the blammy source directory). – DwB Dec 06 '11 at 19:34
-
-
@DwB - I have a related question here: http://stackoverflow.com/q/34980398/1735836 What to put in place of `// stuff as required.` ? I need an explanation for Dummies. – Patricia Jan 24 '16 at 19:33
-
For copy and pasters, change `@Retention` to `RetentionPolicy.RUNTIME` if you want visibility outside of compilation. – markdsievers Jul 10 '17 at 23:35
package-info.java
The package-info.java
is a Java file that can be added to any Java source package. Its purpose is to provide a home for package level documentation and package level annotations.
Simply create the package-info.java
file. Add the package declaration in the file. In fact, the only thing the package-info.java file must contain is the package declaration.
Example of a minimal package info file:
package com.example.myapp.backend.data;
Example of a package-level annotation, @ParametersAreNonnullByDefault
:
@ParametersAreNonnullByDefault
package com.example.myapp.backend.data;
import javax.annotation.ParametersAreNonnullByDefault;
For more info, see the Java specifications, in The Java® Language Specification, section 7.4.1 Named Packages.

- 303,325
- 100
- 852
- 1,154

- 118
- 4
- 10
Open explorer, go to src/your package folder.
right click -> Create new textfile: name it package-info.java.
Go back to eclipse and edit and add the desired content.

- 28,470
- 6
- 53
- 83