Solving the @Resource lookup compilation issue

One common issue that Java EE 6 developers meet is the use of the @Resource annotation which conflicts with the JDK built-in  @javax.annotation.Resource. Here we will show how to solve the compilation issue in Eclipse and Maven.

If you have a look at the @Resource annotation included in Java EE 6, it includes  the lookup attribute. Unfortunately, as we said, the  JDK itself ships its own version of @javax.annotation.Resource which doesn’t have this attribute. This leads to a compilation problem either if you are using Eclipse or Maven.

public class MyCache {
    @Resource(lookup="java:jboss/infinispan/container/cluster")
    private CacheContainer container;
}
[ERROR] symbol  : method lookup()
[ERROR] location: @interface javax.annotation.Resource

Solving the issue in the Eclipse environment is quite easy: all you need to do is adding the JBoss EE 6 implementation of the @Resource annotation – AND – put it ahead of the JDK libraries

@resource maven lookup
The JBoss library is should be named jboss-annotations-api_1.1_spec-1.0.0.Final.jar (or something like that) in the AS 7 release and jboss-annotations-api_1.1_spec-1.0.1.Final-redhat-2.jar in the EAP 6

If you are compiling with Maven, then the simplest way to solve this issue is to add the annotation JAR library into the endorsed dir of your JDK, so that it will override the default @Resource implementation.

The annotation JAR needs to be copied into the JAVA_HOME/jre/lib/endorsed of your JDK distribution.

As an alternative you can pass the  -Djava.endorsed.dirs to the JVM parameters, as shown by the following Maven build from Eclipse

endorsed maven @resource lookup


Recommended Articles

Fixing Maven Version Mismatch Issue in Enterprise Java Applications

Learn how to resolve Maven using incorrect versions during compilation and execution with this guide.

Optimize Java Application Deployment with Maven and WildFly Plugin

Learn how to configure the Maven WildFly plugin for seamless deployment of Java apps. #JavaDev #WildFly #Maven

Fixing Malformed åçè Encoding Error in Maven Builds

Learn how to fix and prevent 'Malformed \uxxxx encoding' errors in your Maven builds with our comprehensive guide. #Maven #Java #CI/CD #CloudNative

Create and Use a Maven Multi-Module Project with EJB Application Example

Learn how to create an EJB application using Maven multi-module projects, including client and server modules. #Maven #JavaEE #WildFly31