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();
}
}
}