Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Get Module and Application Name in JEE Applications

DZone's Guide to

How to Get Module and Application Name in JEE Applications

· Java Zone
Free Resource

Just released, a free O’Reilly book on Reactive Microsystems: The Evolution of Microservices at Scale. Brought to you in partnership with Lightbend.

For those of you who always wanted to get the name of the application or the module at runtime in a JEE 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 JEE 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.

Strategies and techniques for building scalable and resilient microservices to refactor a monolithic application step-by-step, a free O'Reilly book. Brought to you in partnership with Lightbend.

Topics:

Published at DZone with permission of Ralf Quebbemann, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}