2 Replies Latest reply on Mar 27, 2004 11:10 AM by Markus Ide

    Creating java Client for Simple EJB

    vopatel Newbie

      I have a simple EJB HelloWorld.jar and it gets deployed successfully, but when I run a java client for that EJB, I get error as follows:

      *******************************************
      C:\jboss-3.0.4\client>java -classpath .;jboss-j2ee.jar com.vigorsoft.ejb.HelloClient
      in HelloClient
      in HelloClient 1
      in HelloClient 2
      in HelloClient 3
      Exception in thread "main" 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
      at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
      at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
      at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
      at javax.naming.InitialContext.lookup(Unknown Source)
      at com.vigorsoft.ejb.HelloClient.main(HelloClient.java:44)

      **************************************

      My Client and EJB files are as follows:

      ******************************************
      Client.java


      package com.vigorsoft.ejb;

      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.rmi.PortableRemoteObject;

      import java.util.Properties;

      public class HelloClient {

      public static void main(String args[]) throws Exception {
      System.out.println("in HelloClient");
      Properties props = System.getProperties();


      System.out.println("in HelloClient 1");
      Context ctx = new InitialContext(props);
      System.out.println("in HelloClient 2 ");
      //Object ob = ctx.addToEnvironment("HelloHome",HelloHome.class);
      System.out.println("in HelloClient 3 ");
      Object obj = ctx.lookup("HelloHome");
      //I also tried using Object obj = ctx.lookup("Hello"), but same result

      System.out.println("in HelloClient 4");
      HelloHome home = (HelloHome)
      PortableRemoteObject.narrow(
      obj,HelloHome.class);
      System.out.println("in HelloClient 5");
      Hello hello = home.create();
      System.out.println("in HelloClient 6");
      System.out.println(hello.hello());
      }


      }

      ********************************************
      HelloHome.java (home interface)


      package com.vigorsoft.ejb;

      import javax.ejb.EJBHome;
      import java.rmi.RemoteException;
      import javax.ejb.CreateException;
      import javax.ejb.EJBException;

      public interface HelloHome extends EJBHome{

      //This method creates an EJB Object and returns that Object
      Hello create() throws EJBException, RemoteException, CreateException;
      }


      ***************************************
      Hello.java (remote interface)

      package com.vigorsoft.ejb;

      import javax.ejb.EJBObject;
      import java.rmi.RemoteException;

      public interface Hello extends EJBObject{

      // This method returns a greeting to client
      public String hello() throws RemoteException;

      }


      ************************************
      HelloBean.java

      package com.vigorsoft.ejb;

      import java.rmi.RemoteException;

      import javax.ejb.EJBException;
      import javax.ejb.SessionBean;
      import javax.ejb.SessionContext;

      public class HelloBean implements SessionBean {


      public void ejbCreate() throws EJBException, RemoteException {

      System.out.println("ejbCreate()");
      }


      public void ejbActivate() throws EJBException, RemoteException {

      System.out.println("ejbActivate()");
      }


      public void ejbPassivate() throws EJBException, RemoteException {
      System.out.println("ejbActivate()");

      }


      public void ejbRemove() throws EJBException, RemoteException {
      System.out.println("ejbActivate()");

      }


      public void setSessionContext(SessionContext arg0)
      throws EJBException, RemoteException {


      }



      public String hello(){
      System.out.println("in hello method of bean");
      return("Hello World");
      }

      }


      ****************************************
      ejb-jar.xml in META-INF directory parallal to com directory

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">



      <ejb-jar>
      <enterprise-beans>

      <ejb-name>Hello</ejb-name>
      com.vigorsoft.ejb.HelloHome
      com.vigorsoft.ejb.Hello
      <ejb-class>com.vigorsoft.ejb.HelloBean</ejb-class>
      <session-type>Stateless</session-type>
      <transaction-type>Container</transaction-type>

      </enterprise-beans>
      </ejb-jar>
      ***********************************************


      Please help me to solve this error..

      Thanks,
      Vishal