12/08/2009

How to get module name and app name

Java EE 6 brings an easy and portable way to get the module name and application name of the current app, using @Resource field injection or lookup:

    @Resource(lookup="java:module/ModuleName")
private String moduleName;

@Resource(lookup="java:app/AppName")
private String appName;
The above code can be in any Java EE component classes, such as servlet, servlet filter, web listener class, JSP tag handler class, JSF managed bean, EJB bean class, interceptor class, application client main class, and their super classes as well.

You can also look up the module name and app name by their reserved jndi names:
//look up inside a method
String mname = (String) initialContext.lookup("java:module/ModuleName");
String anmae = (String) initialContext.lookup("java:app/AppName");
If you only need moduleName or appName in a particular method, I feel looking them up on-demand is slightly better.

From Java EE 6 forward, the default module name is the base name of a WAR, ejb-jar, or application client jar, that is, the jar name without the .war and .jar extension. One can customize it in descriptors (web.xml, ejb-jar.xml, application-client.xml), though this is rarely needed.

The default app name is the basename of a EAR, i.e., the EAR name without the .ear extension. One can customize it in application.xml.

5 comments:

vitamine a said...

This issue has a partial fix in that grokproject will ask for a "main" module name. I think this is the best we can do in practical terms, as it really is a Python "thing" that having identical module names and package names can lead to import-confusion.

arraylist in java example said...

What a nice tip :) never know getting module name and app name is so easy. thanks a lot man.

How to set Java Classpath in linux

Steve Smith 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

Steve Smith 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