How do I lookup an EJB in a JAR file from a Web application?

This article will teach you how you can look up an EJB (inside a JAR file) from a Web application (inside a WAR file).

Our scenario

The following picture depicts our Deployment scenario:

call ejb from war

Firstly, in order to lookup an EJB packaged as JAR file from another application, for example a Web application you can use the @EJB annotation as follows:

Calculator calculator;

Account account;

In the above example, we are referencing the Calculator Remote interface, whose implementation is CalculatorEJB, available in the application named ejb-server-basic.

Besides, your Web application needs to include in the pom.xml (or the EJB interfaces in order to be able to solve the dependencies.

A more detailed example is available here: WildFly: How to call an EJB from an EJB located in another application

Referencing EJBs through deployment descriptors

If you are coding in EJB2 style (which requires a Home and Remote interface for an EJB), what you need to do is adding the appropriate references to your web.xml and jboss-web.xml.

So if you have an EJB named “HelloBean” add to your web.xml


Then modify jboss-web.xml by adding:


Now you can safely lookup your EJB from your Servlet:

Context ic = new InitialContext();
Object ejbHome = ic.lookup("java:comp/env/HelloBean");