9 Replies Latest reply on Aug 3, 2012 11:25 AM by jayvijayraj

    Unable to use com.sun.jndi.ldap.LdapCtxFactory

    matt.drees

      Hi all,

       

      My project has a piece that needs to read from our ldap.  My code looks something like this:

       

       

      public DirContext createLdapContext()
          {
              Properties env = new Properties();
              env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
              ...
              return new InitialDirContext(env);
          }
      

       

      However, it appears the ModuleClassLoader won't load the com.sun.jndi.ldap.LdapCtxFactory:

       

       

          ...
      Caused by: javax.naming.NamingException: Failed instantiate InitialContextFactory com.sun.jndi.ldap.LdapCtxFactory from classloader ModuleClassLoader for Module "deployment.ccp.war:main" from Service Module Loader
          at org.jboss.as.naming.InitialContextFactoryBuilder.createInitialContextFactory(InitialContextFactoryBuilder.java:58) [jboss-as-naming-7.0.0.Beta4-SNAPSHOT.jar:7.0.0.Beta4-SNAPSHOT]
          at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:664) [:1.6.0_24]
          at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288) [:1.6.0_24]
          at javax.naming.InitialContext.init(InitialContext.java:223) [:1.6.0_24]
          at javax.naming.InitialContext.<init>(InitialContext.java:197) [:1.6.0_24]
          at javax.naming.directory.InitialDirContext.<init>(InitialDirContext.java:82) [:1.6.0_24]
          at org.ccci.ccp.security.LdapContextProducer.createLdapContext(LdapContextProducer.java:59) [classes:]
          ... 65 more
      

       

       

      In digging some, I found the "jboss.modules.system.pkgs" property, and it looks like i could set this to "com.sun.jndi.ldap" and this exception should go away.  Is this the right way to address this?

       

      Thanks!

      -Matt

        • 1. Re: Unable to use com.sun.jndi.ldap.LdapCtxFactory
          matt.drees

          For the record, using this property did solve the problem.

          • 2. Re: Unable to use com.sun.jndi.ldap.LdapCtxFactory
            jason.greene

            That approach will certainly work. The drawback is that it exposes this to all modules, which you don't necessarily want (a big benefit to hiding internal jdk classes is that you know right away when you have a non-portable app).

             

            Another solution we could do is offer a JDK API specific module for the major jdks, and allow you to import those. What do you think?

            • 3. Re: Unable to use com.sun.jndi.ldap.LdapCtxFactory
              matt.drees

              Jason Greene wrote:

               

              Another solution we could do is offer a JDK API specific module for the major jdks, and allow you to import those. What do you think?

               

              Yeah, I would rather use something like that than jboss.modules.system.pkgs, definitely.

              • 4. Re: Unable to use com.sun.jndi.ldap.LdapCtxFactory
                dlofthouse

                There is now a module called com.sun.jndi.ldap that can be used to define a dependency that will give access to theLdapCtxFactory class.

                • 5. Re: Unable to use com.sun.jndi.ldap.LdapCtxFactory
                  matt.drees

                  Great, thanks Darran.

                  • 6. Re: Unable to use com.sun.jndi.ldap.LdapCtxFactory
                    greg.newton

                    Hi Darran,

                     

                    I started developing on Jboss 7.0 a few days ago and ran into this same issue.  Can you give a pointer on the next step now that the module reference above is defined?

                     

                    Thanks!

                    Greg

                    • 7. Re: Unable to use com.sun.jndi.ldap.LdapCtxFactory
                      jensmander

                      Hi,

                       

                      declaring the dependancy in the Manifest-file and using the existing code without any changes worked fine for me.

                       

                      Sascha

                      • 8. Re: Unable to use com.sun.jndi.ldap.LdapCtxFactory
                        matt.drees

                        It looks like the com.sun.jndi.ldap module was removed for 7.0.1.   Referencing instead the sun.jdk module seems to work ok.

                         

                        Just a note for anyone who stumbles on this thread.

                        • 9. Re: Unable to use com.sun.jndi.ldap.LdapCtxFactory
                          jayvijayraj

                          Hi I am facing the same issue with jboss-as-7.1 and I have tried above solution but doesnot work for me.

                           

                          in standalone.xml i have

                          <subsystem xmlns="urn:jboss:domain:ee:1.0">

                                      <global-modules>

                                          <module name="sun.jdk" slot="main"/>

                                      </global-modules>

                                  </subsystem>

                           

                          In the my subsystem module i have in module.xml

                          <dependencies>

                                              <module name="sun.jdk"/>

                          </dependencies>

                           

                          and at my manifest file

                          Dependencies: sun.jdk

                           

                          but still I am getting below error.

                           

                          2012-08-03 16:59:14,312   [ServerService Thread Pool -- 33] ERROR stderr - javax.naming.NamingException: JBAS011843: Failed instantiate InitialContextFactory com.sun.jndi.ldap.LdapCtxFactory from classloader ModuleClassLoader for Module "org.jboss.as.controller:main" from local module loader @1f31c4e (roots: C:\eplatform\jboss-platform\jboss\jboss-as-7.1\modules)

                          2012-08-03 16:59:14,312   [ServerService Thread Pool -- 33] ERROR stderr -           at org.jboss.as.naming.InitialContextFactoryBuilder.createInitialContextFactory(InitialContextFactoryBuilder.java:64)

                          2012-08-03 16:59:14,312   [ServerService Thread Pool -- 33] ERROR stderr -           at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:681) 

                          2012-08-03 16:59:14,312   [ServerService Thread Pool -- 33] ERROR stderr -           at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)

                          2012-08-03 16:59:14,312   [ServerService Thread Pool -- 33] ERROR stderr -           at javax.naming.InitialContext.init(InitialContext.java:242)

                          2012-08-03 16:59:14,327   [ServerService Thread Pool -- 33] ERROR stderr -           at javax.naming.InitialContext.<init>(InitialContext.java:216)

                           

                          please help me what I am doing wrong?

                           

                          regards,

                          Jay

                           

                          PS:

                           

                          Now i have solved it by adding "sun.jdk" dependency into module "org.jboss.as.controller", but question still remains is why it did not found this from global module?