1 Reply Latest reply on Mar 23, 2006 8:14 PM by Datta Mahabalagiri

    java:comp/env problem

    Alex Oscherov Newbie

      Folks,
      sorry for the blast I am new to JBoss and I've got a problem with the small application I wrote just to see how it works. I created simple stateless bean

      Interface One.java:
      package com.documentum.ejb;

      public interface One
      {
      public String sayHello ();
      }

      Implementation OneBean.java:
      package com.documentum.ejb;

      import javax.ejb.Stateless;
      import javax.ejb.Remote;
      import javax.ejb.Local;
      import javax.naming.Context;
      import javax.naming.InitialContext;
      import javax.naming.NamingException;

      @Remote (com.documentum.ejb.One.class)
      @Local (com.documentum.ejb.One.class)
      @Stateless
      public class OneBean
      {
      public String sayHello()
      {
      try
      {
      Context ctx = new InitialContext ();

      String str = (String) ctx.lookup("java:comp/env/frase");


      if (str.length() == 0)
      str = "Nothing in the String";
      return str;
      }
      catch (NamingException e)
      {
      e.printStackTrace();
      }
      return null;
      }
      }

      and my deployment descriptor ejb-jar.xml is:
      <?xml version="1.0" encoding="UTF-8"?>
      <ejb-jar xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
      http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd"
      version="3.0">
      <enterprise-beans>


      <ejb-name>OneEJB</ejb-name>
      <ejb-class>com.documentum.ejb.OneBean</ejb-class>
      <session-type>Stateless</session-type>
      <transaction-type>Container</transaction-type>
      <env-entry>
      <env-entry-name>frase</env-entry-name>
      <env-entry-type>java.lang.String</env-entry-type>
      <env-entry-value>Hello Guys</env-entry-value>
      </env-entry>

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


      To test this bean I use following client:
      import javax.naming.InitialContext;
      import javax.naming.NamingException;

      public class EjbClient
      {
      public static void main(String[] args)
      {
      try
      {
      InitialContext ctx = new InitialContext();
      One stub = (One) ctx.lookup("OneBean/remote");
      System.out.println("Ejb said : " + stub.sayHello());
      }
      catch (NamingException e)
      {
      e.printStackTrace();
      }
      }
      }

      When I install my bean it gets deployed without a problem but when I run my client application I am getting null returned from the bean. When I look into JBoss console I see following error:
      16:37:55,343 ERROR [STDERR] javax.naming.NameNotFoundException: env not bound

      When I debug my bean I see that this exception gets thrown when I call
      String str = (String) ctx.lookup("java:comp/env/frase");

      I would greatly appreciate any help

      Regards

        • 1. Re: java:comp/env problem
          Datta Mahabalagiri Newbie

          I am getting the same error. I created a sample application, as described in tutorial http://docs.jboss.com/jbosside/tutorial/build/en/html/index.html. When I test the application I get

          17:00:15,540 INFO [STDOUT] javax.naming.NamingException: Could not dereference object [Root exception is javax.naming.NameNotFoundException: ejb not bound]

          JNDI name appears correct in configuration files. Here is the Servlet code:

          public void init(ServletConfig config) throws ServletException {
          super.init(config);
          try {
          Context context = new InitialContext();
          Object ref = context.lookup("java:/comp/env/ejb/Fibo");
          home = (FiboHome) PortableRemoteObject.narrow(ref, Fibo.class);
          }
          catch (Exception e) {
          e.printStackTrace(System.out);
          throw new ServletException("Lookup of java:/comp/env/ failed");
          }
          }

          Thanks.
          Datta