In this guide, we’ll walk you through the steps to programmatically obtain the application name and module name of a WildFly application using Java Naming and Directory Interface (JNDI).
For Java Enterprise Edition / Jakarta EE applications, you can retrieve the application name and module name using JNDI. Here’s how:
By using Annotations
When using the Resource annotation, you can get the application and module name just by adding the following resources:
@Resource(lookup="java:app/AppName") private String appName; @Resource(lookup="java:module/ModuleName") private String moduleName;
Using Initial Context Lookup
Without using annotations, you can still use InitialContext lookup to fetch the same application name or module name:
Context ctx = new InitialContext(); String appName = (String) ctx.lookup("java:app/AppName"); String moduleName = (String) ctx.lookup("java:module/ModuleName");
How do you define the Application name?
The Application name can be configured through the deployment descriptors. For example, for an EAR archive:
<?xml version="1.0" encoding="UTF-8"?> <application xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd" version="6"> <application-name>myapplication</application-name> ... </application>
On the other hand, if you don’t provide an XML descriptors, the name will be the artifact name minus the extension (f.e .war).
Conclusion
In this tutorial, we’ve explored how to programmatically retrieve the application name and module name in JBoss EAP using JNDI. By leveraging both annotation-based and context lookup methods, you can easily access these details for better application management. Additionally, we’ve covered how to configure these names in deployment descriptor