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

Microservices. Streaming data. Event Sourcing and CQRS. Concurrency, routing, self-healing, persistence, clustering...learn how Akka enables Java developers to do all this out of the box! 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.

Microservices. Streaming data. Event Sourcing and CQRS. Concurrency, routing, self-healing, persistence, clustering...learn how Akka enables Java developers to do all this out of the box! Brought to you in partnership with Lightbend. 

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}