How To Call EJB From Eclipse IDE

Assuming that you know how to create J2EE modules in eclipse IDE and generating EJB code using Xdoclet. To know more click here .

  1. Open your J2ee client module and make a class in that application client module named “Main”.
  2. Paste following line of code in class.
  3. import java.rmi.RemoteException;import java.util.Properties;
     
    import javax.ejb.CreateException;import javax.naming.Context;
     
    import javax.naming.InitialContext;
     
    import javax.naming.NamingException;
     
    import javax.rmi.PortableRemoteObject;
     
    import org.javatips.statelessEJB.JavaTipsSession;
     
    import org.javatips.statelessEJB.JavaTipsSessionHome;
     
    public class Main {
     
    public static void main(String[] args) {
     
    Properties ht = new Properties();
     
    ht.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
     
    ht.put(Context.PROVIDER_URL,"localhost:1099");
     
    InitialContext jndiContext;
     
    try {
     
    jndiContext = new InitialContext(ht);
     
    Object ref = jndiContext.lookup("JavaTipsSession");
     
    JavaTipsSessionHome home = (JavaTipsSessionHome)PortableRemoteObject.narrow(ref,JavaTipsSessionHome.class);
     
    JavaTipsSession my= home.create();
     
    System.out.println(my.foo("Welcome to javatips tutorial"));
     
    } catch (NamingException e) {
     
    e.printStackTrace();
     
    }
     
    catch (RemoteException e) {
     
    // TODO Auto-generated catch block
     
    e.printStackTrace();
     
    } catch (CreateException e2) {
     
    e2.printStackTrace();
     
    }
     
    }
     
    /* (non-Java-doc)
     
    * @see java.lang.Object#Object()
     
    */
     
    public Main() {
     
    super();
     
    }
     
    }
  4. Now click “Properties” and on “Java Build Path”, Add “SessionEJB” project to class path so that client code can have access to remote and home interfaces.
  5. Press “OK” ensures that J2EE Server is running in IDE. Now “Run As” as Java Application. Result will be displayed on console.