1 Reply Latest reply on Feb 2, 2003 6:13 AM by Bijoy Sivan K

    How to run EJB Client, please help

    hkulaw Newbie

      Hi,

      I am following the "jboss 3 step by step.pdf" to try an jboss + ejb example, but I can't run the HelloWorldClient.java. I got the following errors:

      Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldClient (wro
      ng name: com/mastertech/sample/HelloWorldClient)
      at java.lang.ClassLoader.defineClass0(Native Method)
      at java.lang.ClassLoader.defineClass(ClassLoader.java:502)
      at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
      3)
      at java.net.URLClassLoader.defineClass(URLClassLoader.java:250)
      at java.net.URLClassLoader.access$100(URLClassLoader.java:54)
      at java.net.URLClassLoader$1.run(URLClassLoader.java:193)
      at java.security.AccessController.doPrivileged(Native Method)
      at java.net.URLClassLoader.findClass(URLClassLoader.java:186)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:299)
      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:265)
      at java.lang.ClassLoader.loadClass(ClassLoader.java:255)
      at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:315)

      Here is the code:

      HelloWorldClient.java
      ==========================================
      package com.mastertech.sample;
      import javax.naming.Context;
      import javax.naming.InitialContext;
      import java.util.Hashtable;
      public class HelloWorldClient
      {
      public static void main( String [] args )
      {
      Hashtable env = new Hashtable();
      env.put(Context.INITIAL_CONTEXT_FACTORY,
      "org.jnp.interfaces.NamingContextFactory");
      env.put(Context.PROVIDER_URL, "localhost:1099");
      env.put("java.naming.factory.url.pkgs",
      "org.jboss.naming:org.jnp.interfaces");
      try
      {
      Context ctx = new InitialContext(env);
      Object obj = ctx.lookup( "HelloWorld" );
      HelloWorldHome home =
      (HelloWorldHome)javax.rmi.PortableRemoteObject.narrow(
      obj, HelloWorldHome.class );
      HelloWorld helloWorld = home.create();
      System.out.println( helloWorld.hello());
      helloWorld.remove();
      }
      catch ( Exception e )
      {
      e.printStackTrace();
      System.out.println( "Exception: " + e.getMessage() );
      }
      }
      }

      HelloWorldHome.java
      ===============================
      package com.mastertech.sample;
      /**
      * HelloWorldHome provides the container the means to
      * create and destroy EJB's.
      */
      public interface HelloWorldHome extends javax.ejb.EJBHome
      {
      HelloWorld create() throws java.rmi.RemoteException,
      javax.ejb.CreateException;
      }

      HelloWorld.java
      ==================================
      package com.mastertech.sample;
      /**
      * This is the remote interface that the client calls to
      * have the EJB do the work.
      */
      public interface HelloWorld extends javax.ejb.EJBObject
      {
      public String hello() throws java.rmi.RemoteException;
      }

      HelloWorldBean.java
      ==================================
      package com.mastertech.sample;
      import javax.ejb.SessionContext;
      /**
      * This class is the actual implementation of the business
      * logic. This is the EJB for simplicity's sake.
      */
      public class HelloWorldBean implements javax.ejb.SessionBean
      {
      private SessionContext ctx;
      public void setSessionContext(SessionContext ctx)
      {
      this.ctx = ctx;
      }
      public void ejbRemove()
      {
      System.out.println( "ejbRemove()" );
      }
      public void ejbActivate()
      {
      System.out.println( "ejbActivate()" );
      }
      public void ejbPassivate()
      {
      System.out.println( "ejbPassivate()" ) ;
      }
      /**
      * The method called to display the string "Hello World!"
      * on the client.
      */
      public String hello()
      {
      System.out.println( "hello()" );
      return "Hello World!";
      }
      }

      ejb-jar.xml
      ===========================================
      <?xml version="1.0" encoding="UTF-8"?>
      <ejb-jar>
      JBoss Hello World Application
      <display-name>Hello World EJB</display-name>
      <enterprise-beans>

      <ejb-name>Hello</ejb-name>
      com.mastertech.sample.HelloWorldHome
      com.mastertech.sample.HelloWorld
      <ejb-class>com.mastertech.sample.HelloWorldBean</ejb-class>
      <session-type>Stateless</session-type>
      <transaction-type>Bean</transaction-type>

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

      What is the problem? CLASSPATH? I have tried to include many jar files in it, but still fialed.

      Please help.

      Many Thanks,
      Michael

        • 1. Re: How to run EJB Client, please help
          Bijoy Sivan K Newbie

          Hi Michael,

          Its the problem witht the classpath setting. Set the classpath to the root of the directory where your package resides. Then you can execute
          java com.mastertech.sample.HelloWorldClient

          Please note : You will have to have all the jars in the JBOSS_HOME/client directory in the classpath before running the client program.

          Hope this helps you.