9 Replies Latest reply on Sep 13, 2005 4:11 AM by rrawasi

    javax.naming.NoInitialContextException

    fredo

      Hi,

      I've deployed a bean in JBoss named ProductBean with the jndi name : MyProduct.
      This bean is a container-managed one.

      This bean works against a mysql DB :
      DATABASE : ACCOUNT
      TABLE : ProductBean

      The deployment in JBoss is well done, i mean that the bean is deployed
      and the table is well created.

      I want to access this bean with a client (simple application)
      and the exception which follows is caught when lookup :

      javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
      at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:646)
      at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:246)
      at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:283)
      at javax.naming.InitialContext.lookup(InitialContext.java:350)
      at ProductClient.main(ProductClient.java:12)

      Here's the code and the properties of JBoss ...

      -> Client CODE {
      Context initial = new InitialContext();
      >>> Object objref = initial.lookup("MyProduct");
      }

      ************* files for deploying bean
      --> jboss.xml {

      <enterprise-beans>

      <ejb-name>ProductBean</ejb-name>
      <jndi-name>MyProduct</jndi-name>

      </enterprise-beans>

      }

      --> jaws.xml {

      mySQLDS
      <type-mapping>MySQL SERVER</type-mapping>
      <default-entity>
      <remove-table>true</remove-table>
      </default-entity>
      <type-mappings>
      ....
      }
      --> ejb-jar.xml {
      <ejb-jar>
      no description
      <display-name>ProductBean</display-name>
      <enterprise-beans>

      no description
      <display-name>ProductBean</display-name>
      <ejb-name>ProductBean</ejb-name>
      ProductHome
      Product
      <ejb-class>ProductEJB</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>java.lang.String</prim-key-class>
      False
      <cmp-field>
      ...
      }

      ************** JBoss property files
      -> jboss.jcml {

      org.opentools.minerva.jdbc.xa.wrapper.XADataSourceImpl
      mySQLDS
      jdbc:mysql://myMachine:myPort/ACCOUNT
      user
      password

      }

      -> jndi.properties {
      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
      }

      Can I have some suggestions please :))

        • 1. Re: javax.naming.NoInitialContextException
          raheit00

          Hi,

          you should setup your context before, try this

          System.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
          System.setProperty("java.naming.provider.url", "localhost:1099");

          or place jndi.properties is your project root directory.

          Bye,
          Ralph

          • 2. Re: javax.naming.NoInitialContextException
            fredo

            Thanks a lot, you find the real problem and you find it quickly.

            • 3. Re: javax.naming.NoInitialContextException
              rrawasi

              I have the same problem I try to find jboss.jcml or jndi.properties file but I don't find them, please help me.
              Thanks

              • 4. Re: javax.naming.NoInitialContextException
                darranl

                You need to create the jndi.properties file yourself and put it on the classpath of the client.

                http://wiki.jboss.org/wiki/Wiki.jsp?page=AccessEJBsRemotely

                • 5. Re: javax.naming.NoInitialContextException
                  rrawasi

                  I create that File and put it in my Client classpath, but still I have that error, I'm new to EJB ,I just make Home,Bean class and remote interface + client .
                  Do I need to create jar file from my EJB files, or do I need to do some extra things?
                  Please recommend me some documentation according to help me work with JBOSS IDE and let me compile my EJBs.
                  Thanks

                  • 6. Re: javax.naming.NoInitialContextException
                    darranl

                    Can you please explain your problem fully including relevant code and commands you are executing.

                    This thread we are discussing how the client can access the deployed bean, why are you asking how to compile the EJB?

                    • 7. Re: javax.naming.NoInitialContextException
                      rrawasi

                      Here is my client code that call a simple Statefull Session bean:

                       public static void go() throws Exception{
                      
                       Context ic = new InitialContext();
                       Object o = ic.lookup("Hello");
                       HelloHome home = (HelloHome) PortableRemoteObject.narrow(o,HelloHome.class);
                       Hello helloer = home.create("Test");
                       System.out.println("From EJB: "+helloer.hello());
                       helloer.remove();
                       }
                       public static void main(String[] args)
                       {
                       try{
                       go();
                       }
                       catch(Exception ex){
                       ex.printStackTrace();
                       }
                      
                       }
                      

                      -----------------------------------------------------------------------
                      Here is my jboss.xml :

                      <jboss>
                      
                       <enterprise-beans>
                      
                       <!--
                       To add beans that you have deployment descriptor info for, add
                       a file to your XDoclet merge directory called jboss-beans.xml that contains
                       the <session></session>, <entity></entity> and <message-driven></message-driven>
                       markup for those beans.
                       -->
                      
                       <session>
                       <ejb-name>Hello</ejb-name>
                       <jndi-name>ejb/Hello</jndi-name>
                      
                       </session>
                      
                       </enterprise-beans>
                      
                       <resource-managers>
                       </resource-managers>
                      
                      </jboss>
                      

                      -----------------------------------------------------------------
                      Here is the Bean (I use JBOSS IDE to design it):
                      public class HelloBean implements SessionBean {
                      
                       /**
                       *
                       */
                       private SessionContext context;
                       private String userName;
                       public HelloBean() {
                       super();
                       // TODO Auto-generated constructor stub
                       }
                      
                       /* (non-Javadoc)
                       * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
                       */
                       public void setSessionContext(SessionContext ctx)
                       throws EJBException,
                       RemoteException {
                       context = ctx;
                       System.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
                       System.setProperty("java.naming.provider.url","localhost:1099");
                       }
                      
                       /* (non-Javadoc)
                       * @see javax.ejb.SessionBean#ejbRemove()
                       */
                       public void ejbRemove() throws EJBException, RemoteException {
                       // TODO Auto-generated method stub
                       }
                      
                       /* (non-Javadoc)
                       * @see javax.ejb.SessionBean#ejbActivate()
                       */
                       public void ejbActivate() throws EJBException, RemoteException {
                       // TODO Auto-generated method stub
                       }
                      
                       /* (non-Javadoc)
                       * @see javax.ejb.SessionBean#ejbPassivate()
                       */
                       public void ejbPassivate() throws EJBException, RemoteException {
                       // TODO Auto-generated method stub
                       }
                      
                       /**
                       * Default create method
                       *
                       * @throws CreateException
                       * @ejb.create-method
                       */
                       public void ejbCreate() throws CreateException {
                       // TODO Auto-generated method stub
                       }
                       public void ejbCreate(String name) throws CreateException {
                       userName = name;
                       }
                       /**
                       * Business method
                       * @ejb.interface-method view-type = "remote"
                       */
                       public String hello() {
                       // TODO Auto-generated method stub
                       return "Hello from Stateless Session Bean"+ userName;
                       }
                      }
                      


                      ------------------------------------------------------
                      I get this error:
                      javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
                      at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
                      at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
                      at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
                      at javax.naming.InitialContext.lookup(Unknown Source)
                      at tutorial.client.ClientTest.go(ClientTest.java:30)
                      at tutorial.client.ClientTest.main(ClientTest.java:39)
                      ------------------------------------------------------

                      Thank you for your help, if you think you need any further info please ask me to send them. Because I use Jboss IDE for EJB3 I don't think it is necessary to post my Home and Component interface but if it is necessary please inform me.
                      THANX

                      • 8. Re: javax.naming.NoInitialContextException
                        rrawasi

                        Thank you I just find my problem online in setSessionContext missed.
                        I delete it and add jndi.properties to class path of my project

                        • 9. Re: javax.naming.NoInitialContextException
                          rrawasi

                           

                          "rrawasi" wrote:
                          Thank you I just find my problem online in setSessionContext missed.
                          I delete it and add jndi.properties to class path of my project


                          I mean one line not online