First EJB: Please help with  InitialContext

Posted on 2007-11-23
Last Modified: 2013-12-02
Hi everybody.

I'm trying to jump into J2EE from J2SE.  To start, I'm trying to make my first EJB3.  When I run it, I get:

javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial

I'm running Eclipse Europa, with the JBossTools v4.2 plugin.  I can see that the JBoss server is running.  And I can see that the jar file containing all my example code exists in C:\jboss-4.2.2.GA\server\all\deploy.  When I "run" the client, I'm right-clicking on the source file within Eclipse, and selecting "Run as...Java Application".

As we newbies always say, any help would be greatly appreciated !

Here is the client:

package org.jboss.tutorial.stateless.client;
import javax.naming.*;
import org.jboss.tutorial.stateless.bean.*;

public class ClientSmall {
      public static void main(String[] args) throws Exception {
            Calculator calculator = null;
            try {
                  InitialContext ctx = new InitialContext();
                  calculator = (Calculator) ctx.lookup("Calculator/remote");
            } catch (NamingException e) {
            System.out.println("1 + 1 = " + calculator.add(1, 1));
            System.out.println("1 - 1 = " + calculator.subtract(1, 1));

And here are the other classes:

package org.jboss.tutorial.stateless.bean;
public interface Calculator
   public int add(int x, int y);  
   public int subtract(int x, int y);

package org.jboss.tutorial.stateless.bean;
import javax.ejb.Local;
public interface CalculatorLocal extends Calculator

package org.jboss.tutorial.stateless.bean;
import javax.ejb.Remote;
public interface CalculatorRemote extends Calculator
Question by:McJava1967
LVL 19

Expert Comment

ID: 20352717
But where is your bean itself ?
you should class which looks like :

public class CalculatorBean implements CalculatorRemote, CalculatorLocal{
  public int add(int x, int y){
      return x+y;
   public int subtract(int x, int y){
     return x-y;

Author Comment

ID: 20354053
Sorry.  You're right.  I left that out.  But I do have one here, exactly as you show it.

I should say since I posted this, I've found some more:

When my new JBoss 4.2 Eclipse plugin starts, I'm getting this in the event log:

JMXException: Could not obtain connection to any of these urls: localhost:1099 and discovery failed with error: javax.naming.CommunicationException: Receive timed out [Root exception is Receive timed out]

I've tried switching the port to something like 1234, to be sure it wasn't in use, but I get the same error. The server itself does come up, but it seems I can't use JNDI for this reason.

I'm a newbie at JBoss, so there's a good chance I missed something obvious. If anyone could point me in the right direction on this, I'd really appreciate it. I've been trying to get started here for a couple weeks!


Accepted Solution

DaveyEss earned 500 total points
ID: 20426429
You need to specify some details in the InitialContext so that your application can connect to JBoss.

You need to do something like the following before doing the lookup (changing jboss_ip_address to your server's IP address):

Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
env.put(Context.PROVIDER_URL, "jnp://jboss_ip_address:1099");
InitialContext context = new InitialContext(env);

Open in new window


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

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

840 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