@Resource.lookup and classpath

JavaEE 6 added a new lookup attribute to @javax.annotation.Resource. So one can inject resources like this:

@Resource(lookup = "java:module/env/rate")
private String rate;
Compile the classes:
javac -cp "$GLASSFISH_HOME/modules/*" *Bean.java

EmailBean.java:18: cannot find symbol
symbol : method lookup()
location: @interface javax.annotation.Resource
@Resource(lookup = "java:module/env/rate")
1 error
In GlassFish, all JavaEE classes are in various jars under $GLASSFISH_HOME/modules and its subdirectories. It failed because version 1.0 of javax.annotation.Resource class (and other javax.annotation.* classes) are included in Java SE 6. The use of lookup attribute requires version 1.1 of Common Annotation jar, which is included in JavaEE 6.

To fix it, simply add a -Djava.endorsed.dirs sysprop to override those classes in Java SE 6:
javac -cp "$GLASSFISH_HOME/modules/*" -Djava.endorsed.dirs=$GLASSFISH_HOME/modules/endorsed  *Bean.java
In Eclipse IDE, you just need to put $GLASSFISH_HOME/modules/endorsed/javax.annotation.jar before JRE System Library when configuring Java Build Path.

This should also work with any compliant appserver, though actual jar name and location may vary. The same -Djava.endorsed.dirs sysprop should also be used when running the app.


Frederik Mortensen said...

If you are using Maven, you can also do the following changes to your pom to handle the resource problem:


hedonplay said...

Thanks for your solution.

Anonymous said...

Sun are fucking freaks to come up with different JEE and JSE implementations

Javin said...

You have a wonderful blog man I really like your short and useful tips , this is something I didn't know but looks quite interesting to me.

How classpath works in Java, Setting classpath in Unix and windows

Anna said...

Great and Useful Article.

Online Java Course

Java Online Training

Java Course Online

J2EE training

online J2EE training

Best Recommended books for Spring framework

Java Interview Questions

Java Training Institutes in Chennai

Java Training in Chennai

J2EE Training in Chennai

java j2ee training institutes in chennai

Vũ Diệu Linh said...

I enjoyed on reading your blog post. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. Please visit my website, Friv 4000 Games is where all the free friv games.
Friv 4000