Solved

First EJB: Please help with  InitialContext

Posted on 2007-11-23
3
1,277 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 19

Expert Comment

by:ramazanyich
ID: 20352717
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
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 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
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

0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Running JavaFX on the Raspberry Pi 27 301
Java array 10 89
Bitwise and to sum elements 2 54
JVM error from eclipse 1 27
Upgrading Tomcat – There are a couple of methods to upgrade Tomcat is to use The Apache Installer is to download and unzip and run the services.bat remove|install Tomcat6 Because of the App that we are working with, we can only use Tomcat 6.…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

752 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