7 Replies Latest reply on Jul 8, 2011 4:28 PM by Iván Pazmiño

    JNDI with no jndi.properties

    Iván Pazmiño Novice

      I'm trying to construct an instance of InitialContext with a specific configuration set in a properties' file. This causes the following exception

       

      Caused by: javax.naming.NamingException: Failed instantiate InitialContextFactory org.jnp.interfaces.NamingContextFactory from classloader ModuleClassLoader for Module "deployment.test.jar:main" from Service Module Loader
      

       

      which has been previously recorded in this post JNDI issue in as7. I also did cross this post where is suggested to use JNDI with no jndi.properties. I just can't figure it out how. Any help is mostly welcome.

        • 1. Re: JNDI with no jndi.properties
          Prasad Deshpande Expert

          It's probably that you have jndi.properties in your application classplath & because of which you are getting this exception. Please try to remove it & then try again to build your Context. Context ctx = new InitialContext();

          1 of 1 people found this helpful
          • 2. Re: JNDI with no jndi.properties
            Gerry Matte Apprentice

            I found that a simple way to quickly make your programs work is to comment out all the lines in your jndi.properties file.

            That allows your legacy code to be unchanged and it seems to prevent the NamingException.

             

            Alternatively, you can change the code
            InitialContext ctx = new InitialContext(jndiproperties);
              to instead be
            InitialContext ctx = new InitialContext( );

            1 of 1 people found this helpful
            • 3. Re: JNDI with no jndi.properties
              Iván Pazmiño Novice

              Thank you for your replies. I think I shouldn't have omitted a big detail, I'm trying to get a reference to an EJB2 component deployed in a different server from where I'm trying to consume, so I need to specify the java.naming.provider.url's ip address somewhere and I don't think you can do this using

               

              @EJB(lookup="jndi/properties/including/ip/address")
              
              • 4. Re: JNDI with no jndi.properties
                Gerry Matte Apprentice

                If you want some tested code from one of my servlets:

                DataSource ds;
                Connection con;
                  try {
                    InitialContext ctx = new InitialContext();
                    Context datasourcesCtx = (Context) ctx.lookup("java:jboss/datasources");
                    ds = (DataSource) datasourcesCtx.lookup("ExampleDS");
                    out.println("<p>Success: looked up ExampleDS and cast the resulting Object as a DataSource</p>");
                    con = ds.getConnection();
                    out.println("<p>Success: obtained a JDBC connection from ExampleDS</p>");
                    con.close();
                  }
                  catch (NamingException nex) {System.err.println(nex);out.println("NamingException:<BR>"+nex+"<BR>");}
                  catch (SQLException e) {System.err.println(e);out.println("SQLException:<BR>"+e+"<BR>");}
                  finally {};
                • 5. Re: JNDI with no jndi.properties
                  Iván Pazmiño Novice

                  Thank you. Still, this datasource is installed in the same instance, or same server at least, where your container is deployed. But I need to access a component in a different IP address in a different server. An as4.3 server actually, and, my container is deployed to an as7 in localhost. So, I believe I need to set the java.naming.provider.url somewhere so the service may be found.

                  • 6. Re: JNDI with no jndi.properties
                    Prasad Deshpande Expert

                    Yes, I guess error you are getting is about org.jnp.interfaces.NamingContextFactory being not found is due to the fact that in earlier releases, it used to packaged with client jar, for AS7 it's not yet available in CR1/Final release. But I guess even though I haven't tried, you can still set

                    java.naming.provider.url=<host_name>:<port> property in hashtable & pass it to the contructor of InitialContext & that should ideally work.

                    • 7. Re: JNDI with no jndi.properties
                      Iván Pazmiño Novice

                      Thank you, it did work.

                      What I actually did was just deleting these two entrances from the properties file

                      java.naming.factory.initial

                      java.naming.factory.url.pkgs

                      So I still have the jndi.properties in my app's classpath holding the provider url and a dependency to the jnp client