First EJB: Please help with InitialContext

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
Who is Participating?
DaveyEssConnect With a Mentor Commented:
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

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;
McJava1967Author Commented:
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!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.