3 Replies Latest reply on Jun 30, 2003 4:13 AM by Juha Lindfors

    Hello ! The problem of "naming" !

    jonathan78wong Newbie

      Dear all ,
      Hello ! I write a very sample EJB . The ejb-jar.xml is follow :

      <?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>
      JBoss Hello World Application
      <display-name>Hello World EJB</display-name>
      <enterprise-beans>

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

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

      And the jboss.xml is follow :

      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE jboss-web
      PUBLIC "-//JBoss//DTD Web Application 2.3//EN"
      "http://www.jboss.org/j2ee/dtds/jboss-web_3_0.dtd">


      <enterprise-beans>

      <ejb-name>HelloWorld</ejb-name>
      <jndi-name>HelloWorld</jndi-name>

      </enterprise-beans>


      And a client programme is follow :

      package com.sample;

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

      public class HelloWorldClient {
      public static void main(String[] args){
      try {
      Hashtable env = new Hashtable();
      env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
      env.put(Context.PROVIDER_URL, "localhost");
      env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
      Context lContext = new InitialContext(env);
      Object objref = lContext.lookup("HelloWorld");
      HelloWorldHome lHome = (HelloWorldHome) PortableRemoteObject.narrow(objref, HelloWorldHome.class);
      HelloWorld lSession = lHome.create();
      System.out.println( "" + lSession.hello() );
      }catch( Exception e ) {
      System.out.println( "Caugth exception: " + e.getMessage() );
      e.printStackTrace();
      }
      }
      }

      And the bat for running the client :

      @echo off
      C:\j2sdk1.4.1_02\bin\java %1% -classpath .;\jboss-3.2.1_t\client\jboss-j2ee.jar;\jboss-3.2.1_t\client\log4j.jar;\jboss-3.2.1_t\client\jboss-common-client.jar;\jboss-3.2.1_t\client\jboss-system-client.jar;\jboss-3.2.1_t\client\jnp-client.jar;\jboss-3.2.1_t\client\jboss-client.jar;\jboss-3.2.1_t\client\jbosssx-client.jar;\JBoss_Test\test_EJB\helloWorld.jar

      However , dos says :

      Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldClient (wro
      ng name: com/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)

      I try to call "HelloWorld" , but why dos says "Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldClient (wro
      ng name: com/sample/HelloWorldClient)" ? Thank you !

        • 1. Re: Hello ! The problem of "naming" !
          Juha Lindfors Master

          You've got the wrong class name or Java class packaging in your HelloWorld client class. This is not related to EJB at all. Just normal Java naming.

          Post the content of your client jar and the exact command line you're using trying to start it.

          • 2. Re: Hello ! The problem of "naming" !
            jonathan78wong Newbie

            Dear Juha ,

            Hello ! My HelloWorld client (HelloWorldClient) is follow :

            package com.sample;

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

            public class HelloWorldClient {
            public static void main(String[] args){
            try {
            Hashtable env = new Hashtable();
            env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
            env.put(Context.PROVIDER_URL, "localhost");
            env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
            Context lContext = new InitialContext(env);
            Object objref = lContext.lookup("HelloWorld");
            HelloWorldHome lHome = (HelloWorldHome) PortableRemoteObject.narrow(objref, HelloWorldHome.class);
            HelloWorld lSession = lHome.create();
            System.out.println( "" + lSession.hello() );
            }catch( Exception e ) {
            System.out.println( "Caugth exception: " + e.getMessage() );
            e.printStackTrace();
            }
            }
            }

            And the Bean (HelloWorldBean) is follow :

            import javax.ejb.SessionContext;

            public class HelloWorldBean implements javax.ejb.SessionBean
            {
            private SessionContext ctx;
            public void setSessionContext(SessionContext ctx)
            {
            this.ctx = ctx;
            }
            public void ejbCreate() {
            System.out.println("ejbCreate()");
            }
            public void ejbRemove()
            {
            System.out.println("ejbRemove()");
            }
            public void ejbActivate()
            {
            System.out.println("ejbActivate()");
            }
            public void ejbPassivate()
            {
            System.out.println("ejbPassivate()");
            }

            public String hello()
            {
            System.out.println("hello");
            return "Hello World!";
            }
            }

            And the Home (HelloWorldHome) is follow :

            package com.sample;

            public interface HelloWorldHome extends javax.ejb.EJBHome
            {
            public HelloWorld create() throws java.rmi.RemoteException, javax.ejb.CreateException;
            }

            And the Remote (HelloWorld) is follow :

            package com.sample;

            public interface HelloWorld extends javax.ejb.EJBObject
            {
            public String hello() throws java.rmi.RemoteException;
            }

            And the ejbcl.bat for running the client :

            @echo off
            C:\j2sdk1.4.1_02\bin\java %1% -classpath .;\jboss-3.2.1_t\client\jboss-j2ee.jar;\jboss-3.2.1_t\client\log4j.jar;\jboss-3.2.1_t\client\jboss-common-client.jar;\jboss-3.2.1_t\client\jboss-system-client.jar;\jboss-3.2.1_t\client\jnp-client.jar;\jboss-3.2.1_t\client\jboss-client.jar;\jboss-3.2.1_t\client\jbosssx-client.jar;\JBoss_Test\test_EJB\helloWorld.jar

            Then , I use the command line as follow :

            ejbcl HelloWorldClient

            • 3. Re: Hello ! The problem of "naming" !
              Juha Lindfors Master

              You need to specify the full Java package name: com.sample.HelloWorldClient when you start the application.