Solved

First EJB: Please help with  InitialContext

Posted on 2007-11-23
3
1,260 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
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
splitOdd10 challenge 5 109
Java JRE greater than 1.6 5 63
windows explorer path to command prompt 5 46
jsp insert to database example 2 26
Verbose logging is used to diagnose garbage collector problems. By default, -verbose:gc output is written to either native_stderr.log or native_stdout.log.   It is also possible to redirect the logs to a user-specified file. This article will de…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

832 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