How to get the Java EE Application Name programmatically

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


Recommended Articles

Develop Basic Java EE 8 Application on WildFly with Maven and JBoss Developer Studio

Learn how to develop a basic Java EE 8 application using WildFly, Maven, JSF, CDI, JPA in JBoss Developer Studio. Prerequisites: JDK 1.8 or higher, WildFly, Maven.

Introduction to MongoDB and Interacting with it from JBoss AS

Learn how to set up and interact with MongoDB in a Java application server (JBoss AS). #MongoDB #Java #CloudNative #JBossAS #NoSQL

Configure URL Rewriting with Rewrite in Java EE

Learn how to use Rewrite, a powerful open-source URL rewriting solution for Servlet, JSF, and Java EE applications, to rewrite URLs and improve your application's performance.

Enhance Your Enterprise Java Applications with Global JNDI Entity Manager Binding

Maximize your enterprise applications' performance and scalability by binding Persistence Units globally. Learn how to configure jboss.entity.manager.jndi.name and jboss.entity.manager.factory.jndi.name in persistence.xml for seamless integration.