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

Get the Edge with a Professional Java IDE. 30-day free trial.

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.

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

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 }}