我正在将我的 jdk8 代码升级到 jdk15 并且大多数依赖项和配置设置都可以正常工作,但是我正在努力创建一个新的 InitialDirContext
.
我的组件正在发送邮件并设置 InitialDirContext
获取 MX 记录。当作为主运行时,代码运行得很好,即属性 java.naming.factory.initial
设置为 的创建com.sun.jndi.dns.DnsContextFactory
工作得很好。
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);
}
}
但是,当我在 Felix 中运行这种类型的代码时,我遇到了异常:
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)
我可以看到jdk.naming.dns
模块实际上并没有导出包com.sun.jndi.dns
,但是代码怎么可能在主类中工作?我是否需要将其他设置添加到我的 Felix config.properties 文件中?