2 Replies Latest reply on Jul 20, 2009 5:06 AM by Pablo Carro

    Newbie problem with context lookup

    Pablo Carro Newbie

      Im new here so at first, hi to everybody.

      Im starting with EJB and Jboss and i have solved different problems i had till now but i cant find a solution for this.

      Im building an easy example with 1 easy bean with remote interface with 1 single method that returns a string saying hello world.

      Im trying to access that bean remotely from a simple java client, get the message and print into a dialog.

      Here is the code:

      The EJB remote interface

      package stateless;
      import javax.ejb.Remote;
      public interface NewSessionRemote {
       String getMessage();

      The EJB bean implementing the interface

      package stateless;
      import javax.ejb.Stateless;
      public class NewSessionBean implements NewSessionRemote {
       public String getMessage() {
       return "Hellow World";

      The standalone client

      package standalone;
      import java.util.Properties;
      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.naming.NamingException;
      import stateless.*;
      import javax.swing.*;
      public class Main {
       public static void main(String[] args) {
       try {
       InitialContext ctx = null;
       Properties props = new Properties(System.getProperties());
       props.put(Context.PROVIDER_URL,""); //thats my computer network ip... just for testing purposes. Then i will use a separated file for loading properties
       ctx = new InitialContext(props);
       NewSessionRemote testEJB = (NewSessionRemote) ctx.lookup("MiPrueba/NewSessionBean/remote");
       String cadena= testEJB.getMessage();
       } catch (NamingException nex) {

      Config files are empty. I just used annotations.

      Im using NetBeans as IDE and when i execute my standalone client from NetBeans, everything goes right. A MessageBox showing "Hellow world" appears (the context.lookup returns a proxy) but when i execute the program outside the IDE, i get the following:

      Exception in thread "main" java.lang.ClassCastException: javax.naming.Reference cannot be cast to stateless.NewSessionRemote
       at standalone.Main.main(Main.java:22)

      And changing a bit the code so i can get the classes returned with:

      Object testEJB = ctx.lookup("MiPrueba/NewSessionBean/remote");

      when i execute it inside the IDE i get a : class $Proxy2

      but executing it outside the IDE i get: class javax.naming.Reference

      Any kind of help is welcome and thx in advance. You make it easier for people whos starting with this ;)

      PD: If you need more information just ask for it