8 Replies Latest reply on Dec 4, 2004 7:33 AM by naveen

    calling EJB from the client

    naveen

      I have created a simple entity bean using jboss.
      i have deployed the entity bean.
      now i want to call it from the client.
      while running i get the error.

      javax.naming.NoInitialCotextException Cannot instantiate class:jnp.interfaces.NamingContextFactory.

      my client coding is
      ----------------------------------------------------------------------------
      ackage product.myproduct;

      import javax.ejb.*;
      import javax.naming.*;
      import java.rmi.*;
      import javax.rmi.PortableRemoteObject;
      import java.util.*;


      public class ProductClient
      {
      public static void main(String args[])
      {
      ProductHome home=null;
      System.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
      System.setProperty("java.naming.provider.url", "192.168.1.7:1099");

      try{
      InitialContext jndiContext = new InitialContext();
      home=(ProductHome)PortableRemoteObject.narrow(jndiContext.lookup("ProductHome"),ProductHome.class);

      home.create("123-456-7890","P5-350","350 Mhz pentium");
      /* home.create("123-456-7891","P5-400","400 Mhz pentium","300");
      home.create("123-456-7892","P5-450","450 Mhz pentium","400");
      home.create("123-456-7893","SD-64","64 MB SDRAM","50");
      home.create("123-456-7894","SD-128","128 MB SDRAM","100");*/

      Iterator i=home.findByName("SD-64").iterator();
      System.out.println("These products match the name SD-64:");
      while(i.hasNext())
      {
      Product prod=(Product)PortableRemoteObject.narrow(i.next(),Product.class);
      System.out.println(prod.getDescription());
      }

      /* i=home.findByBasePrice("200").iterator();
      System.out.println("These products match the base price $200");
      while(i.hasNext())
      {
      Product prod=(Product) PortableRemoteObject.narrow(i.next(),Product.class);
      System.out.println(prod.getDescription());
      }*/
      }catch(Exception e)
      {e.printStackTrace();}
      finally{
      if(home != null){
      Iterator i;
      System.out.println("destroying products");
      try{
      i=home.findAllProducts().iterator();
      while(i.hasNext())
      {
      try{
      Product prod=(Product)PortableRemoteObject.narrow(i.next(),Product.class);
      if(prod.getProductID().startsWith("123"))
      {
      prod.remove();
      }
      }catch(Exception e){e.printStackTrace();}
      }
      }catch(Exception e){}
      }
      }
      System.out.println("Hai");

      }
      }


      what r things i have to add.

        • 1. Re: calling EJB from the client
          darranl

          Have you put the jbossall-client.jar on the classpath of the client?

          • 2. Re: calling EJB from the client
            naveen

            but i can't fin it in jboss2

            now i am getting error as
            javax.Naming.NameNotFoundException

            (my Remote Home Interface)StudentHome not bound

            • 3. Re: calling EJB from the client
              darranl

              When posting errors post the full message and stack trace.

              Have a look at the JNDI View in the jmx-console to see what is actually bound.

              Also you only need to narrow the home interface looked up, you don't need to keep narrowing everything else.

              • 4. Re: calling EJB from the client
                naveen

                Sorry,the error is


                javax.Naming.NoInitialContextException : Cannot instantiate class : org.jnp.interfaces.NamingContextFactory [root exception is java.lang.ClassNotFoundException org.jnp.interfaces.NamingContextFactory

                I have changed the client code into a simple one and set all the properties as suggested by many persons but still error is there.

                my client code is
                ------------------------------------------------------------
                package student;

                import javax.ejb.*;
                import javax.naming.*;
                import java.rmi.*;
                import javax.rmi.*;
                import java.util.*;

                public class StudentClient
                {
                public static void main(String args[])
                {
                System.out.println("i am in start");
                try{

                Properties env = new Properties();
                //env.setProperty("InitialContext.INITIAL_CONTEXT_FACTORY","org.jboss.naming");
                env.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
                env.setProperty("java.naming.provider.url", "localhost:1099");
                env.setProperty("java.naming.factory.url.pkgs","org.jboss.naming");

                InitialContext jndiContext = new InitialContext(env);
                StudentHome home=(StudentHome)PortableRemoteObject.narrow(jndiContext.lookup("StudentHome"),StudentHome.class);

                home.create("Ammamn","A01");

                }catch(Exception e){System.out.println("error in client"+e);}

                System.out.println("i am in end");
                }

                }

                ------------------------------------------------------------
                pls clear me
                1)what r the client jars i have to include in classpath.
                2)Is there is any seperate xml file i have to write for client.

                • 5. Re: calling EJB from the client
                  darranl

                  HAVE YOU PUT jbossall-client.jar ON THE CLASSPATH OF THE CLIENT?

                  • 6. Re: calling EJB from the client
                    naveen

                    i can't able to find that jar in my jboss directory

                    • 7. Re: calling EJB from the client
                      darranl

                      It is in the client folder.

                      • 8. Re: calling EJB from the client
                        naveen

                        yes,i have included that also but still now the same error is there.