serviceconfigurationerror:不是子类型

xmd2e60i  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(406)

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

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;

我已经简化了类和包/模块的名称,因为我的项目已经相当大了,我不认为这是必要的。如果您需要任何其他信息,请询问。
编辑:stacktrace

[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$2.next(Unknown Source)
[13:08:24] [Thread-9/WARN]:     at java.base/java.util.ServiceLoader$2.next(Unknown Source)
[13:08:24] [Thread-9/WARN]:     at java.base/java.util.ServiceLoader$3.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$0(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.loadedrealmplagin,b.serviceprovider是 net.lbflabs.dev.realms.survivalrealm.SurvivalRealm

ibrsph3r

ibrsph3r1#

Class 标识基于类的fqn和 ClassLoader 正在加载。
所以这个班 net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.LoadedRealmPlugin 在类加载器1中加载的是另一个类 net.lightbluefoxlabs.dev.multirealm.core.multirealmcore.Realms.LoadedRealmPlugin 在类加载器2中加载。您将得到类播异常或不是子类型异常,因为它们不同(来自jvm的pov)。
在另一个关于你自己的建筑的问题中提到 ClassLoader 你应该通过家长 ClassLoader . 这个 ClassLoader 应该有共享类,以允许构造适当的类层次结构。

相关问题