Skip to content

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

Problem

When using the @Resource annotation in Java projects, you might encounter the following error:

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

Environment

  • Java
  • IntelliJ IDEA

Reason

This error typically occurs due to a missing or incompatible version of the javax.annotation-api dependency. Tomcat 6.0 or later includes this jar package, but if you’re not using Tomcat or using an older version, you might face this issue.

Solution

Solution #1

Add the following dependency to your pom.xml file:

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

Solution #2

This solution is unverified but worth trying if the first one doesn’t work. Add the following plugin configuration to your pom.xml:

pom.xml
<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>

Summary

The NoSuchMethodError: 'java.lang.String javax.annotation.Resource.lookup()' error is commonly caused by missing or incompatible dependencies related to the javax.annotation-api. By adding the correct dependency or configuring the Maven compiler plugin, you can resolve this issue. Always ensure that your project dependencies are up-to-date and compatible with your Java version.

Final Words + More Resources

My intention with this article was to help others who might be considering solving such a problem. So I hope that’s been the case here. If you still have any questions, don’t hesitate to ask me by email: Email me

Here are also the most important links from this article along with some further resources that will help you in this scope:

Oh, and if you found these resources useful, don’t forget to support me by starring the repo on GitHub!