How to  access EJB:s between apps? (test source valid)

Posted on 2004-09-16
Last Modified: 2010-04-01

I got a problem with accessing EJB:s from a different application than they are running under.

My idea is to have 3 apps running on 1 app server:


The idea is that tasks that may be performed by both the main-app and the admin-app are located as EJB:s in the ejb-app. The reason is that if I need to update an EJB I just need to update it at one place, instead of updating all EJB in every app. Do you get my point?

The problem is of course that I can't figure out how to access any EJB:s in the ejb-app from the admin-app or the main-app.

When I execute this code from a Servlet in the main.ear, for example, that tries to locate a EJB in ejb.ear I get the exception below:

      Context context = new InitialContext();

      Object object = context.lookup("java:comp/env/ejb/ejb/beans");

      TestHome testHome = (TestHome)PortableRemoteObject.narrow(object, TestHome.class);

      TestRemote testRemote = (TestRemote)PortableRemoteObject.narrow(testHome.create(), TestRemote.class);

      String output = testRemote.getStringFromFile("applications/ejb/ejb/outputs/output.html");

Communication error: ejb/ejb/beans not found in main, there are no bound values
javax.naming.NameNotFoundException: ejb/ejb/beans not found in main, there are n
o bound values
        at com.evermind._jj.lookup(.:78)
        at com.evermind._bq._ex(.:121)
        at com.evermind._bq.lookup(.:63)
        at javax.naming.InitialContext.lookup(
        at main.servlets.EjbCallerServlet.start(
        at main.servlets.EjbCallerServlet.doGet(
        at javax.servlet.http.HttpServlet.service(
When I execute that code from a Servlet in the ejb.ear it works perfect.

So the problem is how to get access to EJB:s between applications inside a app server.

I have created a zip file with my 3 apps that is ready to use in any server (I hope) if you would like to try it out for your self.

It is possible to download at:

If you are using orion do not forget to add these lines in default-web-site.xml:
      <web-app application="main" name="main" root="/main" />
      <web-app application="admin" name="admin" root="/admin" />
      <web-app application="ejb" name="ejb" root="/ejb" />
...and these lines in server.xml:
      <application name="main" path="../applications/main.ear" />
      <application name="admin" path="../applications/admin.ear" />
      <application name="ejb" path="../applications/ejb.ear" />

When unzip my zip-file with the 3 apps you will find a build.bat file for Windows for each one of the apps. You may need to change the paths depending on where you unzip it if you would like to use the build.bat-files.
My file System looks like:

When everything is installed you should be able to locate your browser to:


Each index.html got a link to a EjbCallerServlet but only the one in the ejb.ear works and response with a output: THAT CALL WORKED!!

In the zip file is also a overview image of files in the 3 apps.

So if you would take the time to test it and try to see if you can find out how to get access to EJB:s in an other app it would be great!

I guess that I just have missed something but I can't figure out what.

BTW if you think that my architecture with shared EJB:s is totaly crazy please tell me why!

Best reagards

Question by:fredand44
LVL 11

Accepted Solution

sompol_kiatkamolchai earned 80 total points
ID: 12081920
I think yor problem is how to lookup EJB that deploy on another application

Context context = new InitialContext();

From the code like this, you will the context environment on that application which does not contains EJB you want to use.

You should use this api

public InitialContext(Hashtable environment)
               throws NamingException

Hashtable env = new Hashtable();    
env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); // you have to alter this to web server you use.
env.put(Context.PROVIDER_URL, "http://somehost:port");    
env.put(Context.SECURITY_PRINCIPAL, username);    
env.put(Context.SECURITY_CREDENTIALS, password);
Context context = new InitialContext(env);

So this api can connect to any ejb deploy in any server on the network.

Author Comment

ID: 12092000

I finally solved how to connect to EJB:s in apps outside the app where they are located. Perhaps I should say that these apps are located at the same webserver.

On the way I received alot of exceptions, but I tracked them down one by one.

I have created a ZIP file that you can download at:

It will be avialible for at least a month, after that you can email me if you would like me to send it to you.

Best regards


Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Display the newly inserted values 3 75
some methods private some are not 8 212
How to Post an If Statement in JSP 3 54
Market Place not working in Eclipse Luna 2 112
For months I had no idea how to 'discover' the IP address of the other end of a link (without asking someone who knows), and it drove me batty. Think about it. You can't use Cisco Discovery Protocol (CDP) because it's not implemented on the ASAs.…
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question