Solved

First EJB: Please help with  InitialContext

Posted on 2007-11-23
3
1,239 Views
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) {
                  e.printStackTrace();
            }
            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;
@Local
public interface CalculatorLocal extends Calculator
{
}


package org.jboss.tutorial.stateless.bean;
import javax.ejb.Remote;
@Remote
public interface CalculatorRemote extends Calculator
{
}
0
Comment
Question by:McJava1967
3 Comments
 
LVL 19

Expert Comment

by:ramazanyich
Comment Utility
But where is your bean itself ?
you should class which looks like :

@Stateless
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;
  }
}
0
 

Author Comment

by:McJava1967
Comment Utility
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 java.net.SocketTimeoutException: 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!


Thanks!
0
 
LVL 4

Accepted Solution

by:
DaveyEss earned 500 total points
Comment Utility
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

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
powerN  challenge 3 47
Passing list of object to Oracle Database Procedure 3 41
Java and GPO 11 46
GUI builder for Eclipse? 8 7
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now