JDK-8337792 : javax.naming.NamingException: Could not resolve a valid ldap host when using LDAP connection in JDK11
  • Type: Bug
  • Component: core-libs
  • Affected Version: 11.0.23
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2024-08-04
  • Updated: 2024-09-12
  • Resolved: 2024-08-27
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 11
11.0.26-oracle masterFixed
Related Reports
Relates :  
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();
            }
        }
}