Search code examples
javajava-17jboss-eap-7srvwildfly-23

Replace com.sun.jndi.dns.DnsContextFactory Wildfly23 JDK17


Our application server is Jboss 7.4 (Wildfly 23) and when I switched from JDK11 to JDK17 I got the runtime exception: Caused by: java.lang.IllegalAccessException: class org.jboss.as.naming.InitialContext cannot access class com.sun.jndi.dns.DnsContextFactory (in module jdk.naming.dns) because module jdk.naming.dns does not export com.sun.jndi.dns to unnamed module @500cd973

Why we use DnsContextFactory is because we want to do a search (from Java) for ldap domains in the SRV records, code in app:

Hashtable<String, String> env = new Hashtable<>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory"); // replace?
env.put("java.naming.provider.url", "dns:");
DirContext ctx = new InitialDirContext(env); // throws exception
Attributes attributes = ctx.getAttributes("_ldap._tcp.mydomain.com", new String[]{"SRV"}); 
Attribute a = attributes.get("SRV");
for (int i = 0; i < a.size(); i++) {
   .
   .
   .
}

I can resolve this by adding VM argument during start-up: JAVA_OPTS="$JAVA_OPTS --add-exports=jdk.naming.dns/com.sun.jndi.dns=ALL-UNNAMED" but I rather not, my understanding is that add-export will fail in the fututer (JDK21?) and I am lokking for a better long term solution.

Edit: If I start my Jboss 7.4 with JDK11 it all works fine, but if I start it with JDK17 the module is now “hidden” and unavailable. Is there another way to search the SRV-records than using DnsContextFactory from com.sun?


Solution

  • I was able to solve this by replacing the code that creates the DirContext to:

        import javax.naming.spi.NamingManager;
    
        DirContext context = (DirContext) NamingManager.getURLContext("dns", new Hashtable<String, Object>());