How to find the JNDI look-up name of EJB programmatically?

Sameer picture Sameer · Mar 10, 2013 · Viewed 10.3k times · Source

I am using EJB 3.1 in my application.

I want to send one EJB's JNDI look-up name in my application to another application so that it can use that at run time & communicate with my EJB. As the look-up name depends upon the name of the ear files deployed on the EJB container & the name of these ear files will be changing every time any changes are done in my application.

What is the procedure of finding out the look-up name of the any EJB programmatically.

Answer

Miljen Mikic picture Miljen Mikic · Mar 11, 2013

First of all, I wouldn't recommend that approach. Why don't you fix JNDI names of your EJBs?

That said, you can call a method InitialContext#list to iterate through the JNDI tree and find out the name of your EJB. There is a nice code example here, so I won't repeat it. An important note - calling ctx.list("") will only give you first level of JNDI tree, so you will have to call that method recursively in order to obtain EJB names. Hint: EJBs are usually deployed under branch ejb.

See also: