I am in the process of upgrading my jdk8 code to jdk15 and have most of the dependencies and config settings working but I am struggling with the creation of a new InitialDirContext
.
My component is sending out a mail and is setting up the InitialDirContext
to get hold of the MX records. The code runs just fine when run as a main, i.e. the creation with property java.naming.factory.initial
set to com.sun.jndi.dns.DnsContextFactory
works just fine.
public static void main(String[] args) {
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
try {
InitialDirContext initialDirContext = new InitialDirContext(env);
Attributes attrs = initialDirContext.getAttributes("gmail.com", new String[]{"MX"});
Attribute attr = attrs.get("MX");
Set<String> hostNames = new HashSet<>();
if (attr != null) {
for (int i = 0; i < attr.size(); i++) {
String mxAttr = (String) attr.get(i);
String[] parts = mxAttr.split(" ");
hostNames.add(parts[1].substring(0, parts[1].length() - 1));
addIpAddresses(initialDirContext, parts[1], hostNames);
}
}
System.out.println("hostNames = " + hostNames.stream().sorted().collect(Collectors.joining(", ")));
} catch (NamingException e) {
e.printStackTrace(System.err);
}
}
However, when I run this type of code inside Felix I am getting the exception:
Caused by: javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.jndi.dns.DnsContextFactory [Root exception is java.lang.IllegalAccessException: class javax.naming.spi.NamingManager (in module java.naming) 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 module java.naming]
at java.naming/javax.naming.spi.NamingManager.getFactory(NamingManager.java:749)
at java.naming/javax.naming.spi.NamingManager.lambda$getInitialContext$1(NamingManager.java:711)
at java.base/jdk.internal.loader.AbstractClassLoaderValue$Memoizer.get(AbstractClassLoaderValue.java:329)
at java.base/jdk.internal.loader.AbstractClassLoaderValue.computeIfAbsent(AbstractClassLoaderValue.java:205)
at java.naming/javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:711)
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.InitialContext.<init>(InitialContext.java:208)
at java.naming/javax.naming.directory.InitialDirContext.<init>(InitialDirContext.java:130)
I can see that the jdk.naming.dns
module does not actually export the package com.sun.jndi.dns
, but then how is it possible that the code works in a main class? Is there perhaps an additional setting that I need to add to my Felix config.properties file?