Total Pageviews

Tuesday, June 10, 2014

How to get module name and application name in Java EE applications

It's been a long time since my last blog post, but here is an update :-)

For those of you who always wanted to get the name of the application or the module at runtime in a Java EE application, here is a solution:

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

 @Resource(lookup="java:app/AppName")  
 private String applicationName;  

It's been there since Java EE 6 and it's easy and portable!

There is also the opportunity to get those names via JNDI lookup

 String myModuleName = (String) initialContext.lookup("java:module/ModuleName");  
 String myApplicationName = (String) initialContext.lookup("java:app/AppName");  

If you don't need the names in a field but just in a single method this is the way to go.

The default module name is the base name of an ejb-jar or WAR archive.
The default application name is the base name of an EAR archive.

No comments:

Post a Comment