Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8337792

javax.naming.NamingException: Could not resolve a valid ldap host when using LDAP connection in JDK11

    XMLWordPrintable

Details

    • master

    Backports

      Description

        Sample code below combing with user's module-inf.java. It will throw the exception as in the summary below,
        ====================================
        Exception in thread "main" javax.naming.NamingException: Could not resolve a valid ldap host
        at java.naming/com.sun.jndi.ldap.LdapCtxFactory.getContextFromEndpoints(LdapCtxFactory.java:187)
        at java.naming/com.sun.jndi.ldap.LdapCtxFactory.lambda$getUsingURL$0(LdapCtxFactory.java:197)
        at java.base/java.security.AccessController.doPrivileged(Native Method)
        at java.base/java.security.AccessController.doPrivilegedWithCombiner(AccessController.java:570)
        at java.naming/com.sun.jndi.ldap.LdapCtxFactory.getUsingURL(LdapCtxFactory.java:195)
        at java.naming/com.sun.jndi.ldap.LdapCtxFactory.getUsingURLs(LdapCtxFactory.java:241)
        at java.naming/com.sun.jndi.ldap.LdapCtxFactory.getLdapCtxInstance(LdapCtxFactory.java:160)
        at java.naming/com.sun.jndi.ldap.LdapCtxFactory.getInitialContext(LdapCtxFactory.java:90)
        at java.naming/javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:730)
        at java.naming/javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:305)
        at java.naming/javax.naming.InitialContext.init(InitialContext.java:236)
        at java.naming/javax.naming.ldap.InitialLdapContext.<init>(InitialLdapContext.java:154)
        ===============================

        The root cause is that module java.naming is depending on jdk.naming.ldap. Without loading this module, the initialization of LdapDnsProvierService will fail.

        Sample code to duplicate it.

        module JDK11 {
        requires java.naming;
        }

        public class LdapContextTest {

        public static void main(String ...strings) {
        InitialLdapContext ctx = null;
                try {
                    String url = "ldap://localhost:389";
                    Hashtable<String, String> env = new Hashtable<>();
                    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
                    env.put(Context.PROVIDER_URL, url);
                    ctx = new InitialLdapContext(env, null);

                } catch (Exception ex) {
                    if (ex instanceof NamingException) {
                        throw new NamingException("The DNS provider is not loaded.");
                    }
                } finally {
                    if (ctx != null) {
                        ctx.close();
                    }
                }
        }

        Attachments

          Issue Links

            Activity

              People

                wxiao Weibing Xiao
                shadowbug Shadow Bug
                Votes:
                0 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved: