java—如何在运行在jdk15之上的felix(v603)内部的osgi组件中创建initialdircontext

ckocjqey  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(443)

我正在将我的jdk8代码升级到jdk15,并使大多数依赖项和配置设置正常工作,但我正在努力创建一个新的 initialdircontext。
我的组件正在发送邮件并正在设置 initialdircontext获取mx记录。当作为main运行时,代码运行正常,即使用属性创建 "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文件中添加其他设置?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题