others-how to solve NoSuchMethodError: 'java.lang.String javax.annotation.Resource.lookup()'

Problem

When we use @Resource in java projects, sometimes, we get this error:

java.lang.NoSuchMethodError: 'java.lang.String javax.annotation.Resource.lookup()'

Environment

  • java
  • Intellij IDEA

Reason

Tomcat 6.0 or later has included this jar package.

Solution

Solution #1:

Add the following dependencies:

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

Solution #2:

Not verified, but you can try:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    <configuration>
        <release>9</release>
        <compilerArgs>
            <arg>--add-modules</arg>
            <arg>java.xml.ws.annotation</arg>
        </compilerArgs>
    </configuration>
</plugin>

-->