9 Replies Latest reply on Mar 30, 2009 12:04 PM by peterj

    access ejb frm a standalone java client:java.lang.ClassNotFo

    yhqian99

      Hello All,

      I am using MyEclipse5.5 + JBOSS to develop a stateless HelloEJB, after deployment, I can see the deployed bean from http://localhost:8080/jmx-console/.

      Then I use a standalone java client to access the EJB, but get the error of "javax.naming.NoInitialContextException: Cannot instantiate class: org.jnp.interfaces.NamingContextFactory [Root exception is java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory]"


      Here is my code:

      package com;

      import javax.ejb.Stateless;

      @Stateless
      public class HelloEJB implements HelloEJBLocal, HelloEJBRemote
      {
      public HelloEJB()
      {
      }
      public String sayHello(String name)
      {
      return "Welcome, " + name + ", I am a stateless Session Bean";
      }
      }


      package com;

      import javax.ejb.Local;

      @Local
      public interface HelloEJBLocal
      {
      public String sayHello(String name);
      }

      package com;

      import javax.ejb.Remote;

      @Remote
      public interface HelloEJBRemote
      {
      public String sayHello(String name);
      }

      I have set the jbossall-client.jar to my java build path, is there anything I missed? Any help are highly apprieciated.

      Yhqian

        • 1. Re: access ejb frm a standalone java client:java.lang.ClassN
          peterj

          Did you also include jbossall-client.jar in your client's classpath?

          • 2. Re: access ejb from a standalone java client: java.lang.Clas
            yhqian99

            yes, I did include jbossall-client.jar in my client's classpath

            • 3. Re: access ejb frm a standalone java client:java.lang.ClassN
              yhqian99

              I found I must include the JBOSS_HOME\client\jbossall-client.jar into the client's classpath. But previously I copied the jbossall-client.jar from JBOSS_HOME\client to my Myeclipse project, and includes this copy to my client's classpath, this dosen't work.

              Can anybody explain why I must include the JBOSS_HOME\client\jbossall-client.jar instead of a copy? In an application, normally the client wouldn't be at the same machine as the server, so the client couldn't include the jbossall-client.jar at the server side, that's why I made a copy of jbossall-client.jar from the server.

              • 4. Re: access ejb frm a standalone java client:java.lang.ClassN
                jaikiran

                 

                Can anybody explain why I must include the JBOSS_HOME\client\jbossall-client.jar instead of a copy?


                You can create a copy and add it to the classpath. How exactly did you create the copy and how did you add it to the classpath?

                • 5. Re: access ejb frm a standalone java client:java.lang.ClassN
                  yhqian99

                  Hi jaikiran,

                  In MyEclipse I created a EJB project , name it FirstEJB, the three java files of bean are under src\com directory, and the client code are under src\test directory, then I ceated a folder lib under FirstEJB and copy the jbossall-client.jar to lib folder, then through the properties of the FirstEJB project, I add the lib\jbossall-client.jar to the java Build Path Libraries, that's it. After finishing the above steps, my EJB project looks like the following:

                  FirstEJB
                  |---src
                  | |--com
                  | | |---HelloEJB.java
                  | | |---HelloEJBLocal.java
                  | | |---HelloEJBRemote.java
                  | |---test
                  | | |---TestHelloEJB.java
                  | |---META-INF
                  |---JRE System Library[jdk6]
                  |---Java EE 5 Libraries
                  |---jbossall-client.jar
                  |---lib


                  Please note that, jbossall-client.jar is actually pasted into the lib directory,, but after I set the \lib\jbossall-client.jar to the java build path, jbossall-client.jar file appears above the lib directory.


                  If I set the JBOSS_HOME\client\jbossall-client.jar to the project's build path, everything goes well. The project structure will change a little, like the following:

                  FirstEJB
                  |---src
                  | |--com
                  | | |---HelloEJB.java
                  | | |---HelloEJBLocal.java
                  | | |---HelloEJBRemote.java
                  | |---test
                  | | |---TestHelloEJB.java
                  | |---META-INF
                  |---JRE System Library[jdk6]
                  |---Java EE 5 Libraries
                  |---jbossall-client.jar - D:\jboss-5.0.1.GA\client\

                  In this case, the jbossall-client.jar is the one under the server's client directory

                  • 6. Re: access ejb frm a standalone java client:java.lang.ClassN
                    yhqian99


                    Here is my client code:


                    package test;

                    import javax.naming.*;
                    import java.util.*;
                    import com.*;
                    public class TestHelloEJB
                    {
                    public static void main(String[] args)
                    {
                    //JBoss' default remote jndi: <ejb-name>/remote
                    final String jndiName = "HelloEJB/remote";
                    try
                    {
                    Properties p = new Properties();
                    p.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
                    p.put("java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");
                    p.put("java.naming.provider.url", "localhost:1099");
                    InitialContext ctx = new InitialContext(p);

                    HelloEJBRemote helloEJB = (HelloEJBRemote)ctx.lookup(jndiName);
                    String message = helloEJB.sayHello("Tom");
                    System.out.println( message);
                    }catch( NamingException e)
                    {
                    e.printStackTrace();
                    }

                    }

                    }

                    • 7. Re: access ejb frm a standalone java client:java.lang.ClassN
                      peterj

                      Please be aware that in JBoss AS 5.0.x, jbossall-client.jar does not contain any classes. Instead, it references various other JAR files in the jboss_home/client directory. This probably explains the differences you are seeing.

                      • 8. Re: access ejb frm a standalone java client:java.lang.ClassN
                        yhqian99

                        Why did JBoss AS 5.0.x make this change to jbossall-client.jar , it seems not too many people know this change. Since this jbossall-client.jar references various other JAR files, if I want to copy this jbossall-client.jar from JBoss AS 5.0.x, what are the rest jar files should I copy with it?

                        Thanks

                        • 9. Re: access ejb frm a standalone java client:java.lang.ClassN
                          peterj

                          The easy thing is to copy all of client/*.jar. Another alternative is to look at the manisfest.mf file within jbossall-client.jar and copy only the files mentioned. A third option is to copy only the JAR files you need for your app. Yes, I know, having all the necessary classes within jbossall-client.jar was convenient so that you did not have to think about such things. By the way, check out JBoss Tattle - it will give you a list of JAR file dependencies which should made it fairly easy to determine which JAR files you will need.

                          The JBoss team made this change because of the difficulty in keeping the contents of jbossall-client.jar in synch with the rest of the JAR files, especially in the common situation when an updated component (such as EJB3 or messaging) is released and suddenly the classes packaged in jbossall-client.jar were not compatible with the classes in the updated JAR files. Having jbossall-client.jar not contain the classes but instead just reference the JAR files solves this problem.