JBoss - cant get initialcontext in client (newbie)

Discussions

EJB design: JBoss - cant get initialcontext in client (newbie)

  1. Hi, I am a total newbie to the world of EJBs and am just trying out my first one. I am working with JBoss 3.2.2 with Tomcat on my Linux box (Red Hat 8). However, I am having a lot of trbl getting the client code to work. Here is the dump of the exception that I get whenever I run the client (I have tried to look for answers everywhere and have tried to do the things that others did when they had similar problems but with no luck. So I am sorry this is not the first time this question is on this forum)

    Exception in thread "main" javax.naming.NoInitialContextException: Cannot instantiate class: org.jnp.interfaces.NamingContextFactory [Root exception is java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory]
            at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:652)
            at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:243)
            at javax.naming.InitialContext.init(InitialContext.java:219)
            at javax.naming.InitialContext.<init>(InitialContext.java:195)
            at examples.HelloClient.main(HelloClient.java:30)
    Caused by: java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory
            at java.net.URLClassLoader$1.run(URLClassLoader.java:199)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
            at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
            at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
            at java.lang.Class.forName0(Native Method)
            at java.lang.Class.forName(Class.java:219)
            at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:42)
            at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:649)
            ... 4 more

    Here's a part of my client code:

    ...
    Properties props = System.getProperties();
    Context ctx = new InitialContext(props);
    Object obj = ctx.lookup("HelloHome");
    ...

    My jndi.properties file:

    java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
    java.naming.provider.url=jnp://localhost:1099
    java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces.jnp

    My ejb-jar.xml file:

    <?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>
          <session>
             <ejb-name>Hello</ejb-name>
             <home>hellopackage.HelloHome</home>
             <remote>hellopackage.Hello</remote>
             <local-home>hellopackage.HelloLocalHome</local-home>
             <ejb-class>hellopackage.HelloBean</ejb-class>
             <session-type>Stateless</session-type>
             <transaction-type>Container</transaction-type>
            </session>
        </enterprise-beans>
    </ejb-jar>


    I have included the EJB jar file, jboss-j2ee.jar and jnp-client.jar files in my classpath when i run the client. Can anyone help me with this?

    Thanks
  2. Hi,

    Try to put the jars from jboss/client in your client's classpath,
    particularly jnp-client.jar?

    Nabil BENMIRA
  3. Nabil is right. You must include the jars from JBoss/Client in the CLASSPATH along with your client, but that is not all. When you run your client you must also add this to the VM parameters:
    -Djava.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
    -Djava.naming.provider.url=localhost
    And that's all. You don't need any jndi.properties file or something, rather you just write:

    Context ctx = new InitialContext();//without any parameters in the constructor
    HelloHome home = (HelloHome)ctx.lookup("HelloHome");//no need of rmi
    //casting, because this is absolutely unnecessary
    ......

    I have been working with JBoss for half an year and have no problems with this approach.
  4. Thanks Georgi... it worked... the first thing I did was to change the constructor call to work without any parameters and secondly,I added the jbossall-client.jar to my classpath .. and it worked like a charm.... thanks guys