ServiceConfigurationError: not a subtype

ServiceConfigurationError: not a subtype

我从事一个相当复杂的项目已经有一段时间了。它涉及 2 个模块,net.lightbluefoxlabs.dev.multirealm.core.multirealmcorenet.lbflabs.dev.realms.survivalrealm,其中第一个有一个抽象 class, 本身派生自另一个 class (也许这就是问题所在?)。尝试为 class 创建服务时,我收到此错误: java.util.ServiceConfigurationError: net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.LoadedRealmPlugin: net.lbflabs.dev.realms.survivalrealm.SurvivalRealm not a subtype,即使它实际上扩展了正确的 class。这是加载服务的代码:

File file = new File("Dir/plugin_jar.jar");
URLClassLoader c = new URLClassLoader(new URL[]{file.getAbsoluteFile().toURI().toURL()});
ServiceLoader<LoadedRealmPlugin> loader = ServiceLoader.load(LoadedRealmPlugin.class, c);
LoadedRealmPlugin p = loader.iterator().next(); // Throws the exception
return p;

我把class和packet/module名字简化了,因为我的项目已经很大了,我觉得没必要。如果您需要任何其他信息,请询问。

编辑: 堆栈跟踪

[13:08:24] [Thread-9/WARN]: Exception in thread "Thread-9" java.util.ServiceConfigurationError: net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.LoadedRealmPlugin: net.lbflabs.dev.realms.survivalrealm.SurvivalRealm not a subtype
[13:08:24] [Thread-9/WARN]:     at java.base/java.util.ServiceLoader.fail(Unknown Source)
[13:08:24] [Thread-9/WARN]:     at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNextService(Unknown Source)
[13:08:24] [Thread-9/WARN]:     at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNext(Unknown Source)
[13:08:24] [Thread-9/WARN]:     at java.base/java.util.ServiceLoader.next(Unknown Source)
[13:08:24] [Thread-9/WARN]:     at java.base/java.util.ServiceLoader.next(Unknown Source)
[13:08:24] [Thread-9/WARN]:     at java.base/java.util.ServiceLoader.next(Unknown Source)
[13:08:24] [Thread-9/WARN]:     at net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.LoadedRealmPlugin.RealmFactory(LoadedRealmPlugin.java:22)
[13:08:24] [Thread-9/WARN]:     at net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.LoadedRealmPlugin.RealmFactory(LoadedRealmPlugin.java:32)
[13:08:24] [Thread-9/WARN]:     at net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.UnloadedRealm.GetLoadedRealm(UnloadedRealm.java:25)
[13:08:24] [Thread-9/WARN]:     at net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.UnloadedRealm.Load(UnloadedRealm.java:39)
[13:08:24] [Thread-9/WARN]:     at net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.RealmBase.Initialize(RealmBase.java:33)
[13:08:24] [Thread-9/WARN]:     at net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.RealmBase.<init>(RealmBase.java:25)
[13:08:24] [Thread-9/WARN]:     at net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.UnloadedRealm.<init>(UnloadedRealm.java:18)
[13:08:24] [Thread-9/WARN]:     at net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.RealmManager.Initialize(RealmManager.java:25)
[13:08:24] [Thread-9/WARN]:     at net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.RealmManager.lambda$InitializeAsync[=11=](RealmManager.java:42)
[13:08:24] [Thread-9/WARN]:     at java.base/java.lang.Thread.run(Unknown Source)

注意 A.ServiceTemplate 是 net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.LoadedRealmPlugin 而 B.ServiceProvider 是 net.lbflabs.dev.realms.survivalrealm.SurvivalRealm

Class 身份基于 class 正在加载的 ClassLoader 的 FQN。

因此 class net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.LoadedRealmPlugin 在 classloader 1 中加载的 class 与在 classloader 2 中加载的 net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.LoadedRealmPlugin 不同。您将得到 classcast 异常或不是子类型异常,仅仅是因为它们不相同(来自 JVM 的 pov)。

中关于构建您自己的 ClassLoader 中所述,您应该传递父级 ClassLoader。此 ClassLoader 应该具有共享的 classes 以允许构建适当的 classes 层次结构。