39

Ok, so this is probably a NooB question (I'm more of a C++ guy), but I'm lost in the java woods and its frameworks forests...

I'm trying to look into eclipse RCP development. For that I'm following this well-known tutorial: http://www.vogella.com/tutorials/EclipseRCP/article.html

At step 15 I need to add the following dependency packages to import in my bundle. javax.annotation javax.injection

The problem is that I cannot select these (they are not in the selection list) I do have javax.el javax.servlet.* and javax.xml.*

Looking at http://docs.oracle.com/javase/7/docs/api/overview-summary.html suggests that this should be part of the standard java.

What obvious mistake am I missing?

Respect2All
  • 463
  • 1
  • 4
  • 9

6 Answers6

20

The dependency including version:

<dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>javax.annotation-api</artifactId>
    <version>1.3.2</version>
</dependency>

See: http://mvnrepository.com/artifact/javax.annotation/javax.annotation-api

Or for the newer jakarta.annotation:

<dependency>
   <groupId>jakarta.annotation</groupId>
   <artifactId>jakarta.annotation-api</artifactId>
   <version>1.3.5</version>
</dependency>

See: https://mvnrepository.com/artifact/jakarta.annotation/jakarta.annotation-api

The Java Common Annotations Module java.xml.ws.annotation was deprecated in Java version 9 and was removed in java version 11. If this leads to a problem you could try to add javax.annotation.

The Javadocs for Java 8 can be found here: http://docs.oracle.com/javase/8/docs/api/javax/annotation/package-summary.html

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
Stefan Großmann
  • 866
  • 9
  • 20
  • I've tried to import the "guava" library from here: https://github.com/google/guava , but I keep getting errors about the "javax.annotation*" missing, even though I've set the compiler to be of version 8 of Java. How come it's still missing? Can you please help on that? – android developer Apr 10 '15 at 08:23
6

Your comment indicates this is for Guava, so you want the JSR305 library, which extends the javax package.

2020 Update: Note that this library breaks the Oracle licensing agreement and Guava has since moved to checkerframework's Nullable annotation.

Alice Purcell
  • 12,622
  • 6
  • 51
  • 57
5

Not sure if this is still relevant, but for Java 8, I had to add the two following Maven dependencies in order to get javax.annotation.concurrent.ThreadSafe to work:

    <dependency>
        <groupId>javax.annotation</groupId>
        <artifactId>javax.annotation-api</artifactId>
        <version>1.3.2</version>
    </dependency>

    <dependency>
        <groupId>com.google.code.findbugs</groupId>
        <artifactId>jsr305</artifactId>
        <version>3.0.0</version>
    </dependency> 
Dr. Div
  • 951
  • 14
  • 26
2

Javax annotations include in this dependency. This is latest version at the moment.

For maven

<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
</dependency>

For gradle

compile('com.google.code.findbugs:jsr305:3.0.2')

enter image description here

1

In Java version >= 6, you should not need to add them explicitly.
They are part of the JDK. Just try to skip adding them, maybe
the list of instructions is outdated.

Before Java 6, you would have needed to add this jar, I think: jsr250-api-1.0.jar.

http://central.maven.org/maven2/javax/annotation/jsr250-api/1.0/

http://download.java.net/maven/2/javax/annotation/jsr250-api/1.0/

peter.petrov
  • 38,363
  • 16
  • 94
  • 159
0

In case you are looking for nullable annotations, JSpecify is the modern way to go.

They have @Nullable

static void print(@Nullable String x) {
  System.out.println(String.valueOf(x));
}

More interestingly, they have @NullMarked, which states that the whole thing is checked for nullness. If there is no @Nullable marking, the assurance is that there is no null appearing.

@NullMarked
public class Strings {
  public static String nullToEmpty(@Nullable String x) {
    return (x == null) ? "" : x;
  }

  public static int spaceIndex(String x) {
    return x.indexOf(' ');
  }
}
koppor
  • 19,079
  • 15
  • 119
  • 161