注册bot bean的代码
@Bean
public Bot bot(){
//创建bot配置类
BotConfiguration botConfiguration = new BotConfiguration();
//设置配置
botConfiguration.setCacheDir(new File(BOT_PATH)); //BOT_PATH 为定义好的常量
botConfiguration.setHeartbeatStrategy(BotConfiguration.HeartbeatStrategy.REGISTER);
botConfiguration.setProtocol(BotConfiguration.MiraiProtocol.IPAD);
botConfiguration.setWorkingDir(new File(BOT_PATH));
File logdir = new File(BOT_PATH+"log");
if (!logdir.isDirectory()) logdir.mkdirs();
botConfiguration.redirectBotLogToDirectory(logdir);
botConfiguration.redirectNetworkLogToDirectory(logdir);
botConfiguration.fileBasedDeviceInfo();
botConfiguration.enableContactCache();
botConfiguration.getContactListCache().setSaveIntervalMillis(60000*30);
//配置完成,注册bot
Bot bot = BotFactory.INSTANCE.newBot(QQ,PASSWORD,botConfiguration); //QQ,PASSWORD也是定义好的常量
//注册监听 messageListener需要继承SimpleListenerHost类
bot.getEventChannel().parentScope(messageListener).registerListenerHost(messageListener);
return bot;
}
在2.12.3包括之前的任意版本都可以正常工作,但是更新至2.13.x之后的任何版本,都会出现
Caused by: java.util.ServiceConfigurationError: net.mamoe.mirai.IMirai: Provider net.mamoe.mirai.internal.MiraiImpl could not be instantiated
at java.base/java.util.ServiceLoader.fail(ServiceLoader.java:586) ~[na:na]
at java.base/java.util.ServiceLoader$ProviderImpl.newInstance(ServiceLoader.java:813) ~[na:na]
at java.base/java.util.ServiceLoader$ProviderImpl.get(ServiceLoader.java:729) ~[na:na]
at java.base/java.util.ServiceLoader$3.next(ServiceLoader.java:1403) ~[na:na]
at kotlin.collections.CollectionsKt___CollectionsKt.firstOrNull(_Collections.kt:270) ~[kotlin-stdlib-1.5.10.jar:1.5.10-release-890 (1.5.10)]
at net.mamoe.mirai.utils.ServicesKt.loadService(Services.kt:18) ~[mirai-core-utils-jvm-2.13.0.jar:2.13.0]
at net.mamoe.mirai.Mirai.findMiraiInstance(IMirai.kt:357) ~[mirai-core-api-jvm-2.13.0.jar:2.13.0]
at net.mamoe.mirai._MiraiInstance.get(IMirai.kt:348) ~[mirai-core-api-jvm-2.13.0.jar:2.13.0]
at net.mamoe.mirai.Mirai.getInstance(IMirai.kt:39) ~[mirai-core-api-jvm-2.13.0.jar:2.13.0]
at net.mamoe.mirai.internal.message.MessageSerializersImpl.getSerializersModule(MessageSerializersImpl.kt:65) ~[mirai-core-api-jvm-2.13.0.jar:2.13.0]
at net.mamoe.mirai.internal.message.MessageSerializersImpl.registerSerializers(MessageSerializersImpl.kt:76) ~[mirai-core-api-jvm-2.13.0.jar:2.13.0]
at net.mamoe.mirai.message.MessageSerializers$INSTANCE.registerSerializers(MessageSerializers.kt) ~[mirai-core-api-jvm-2.13.0.jar:2.13.0]
at net.mamoe.mirai.internal.message.protocol.MessageProtocolFacade.<clinit>(MessageProtocolFacade.kt:165) ~[mirai-core-jvm-2.13.0.jar:2.13.0]
at net.mamoe.mirai.internal.MiraiImplKt._MiraiImpl_static_init(MiraiImpl.kt:25) ~[mirai-core-jvm-2.13.0.jar:2.13.0]
at net.mamoe.mirai.internal.MiraiImpl.<clinit>(MiraiImpl.kt:87) ~[mirai-core-jvm-2.13.0.jar:2.13.0]
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) ~[na:na]
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480) ~[na:na]
at java.base/java.util.ServiceLoader$ProviderImpl.newInstance(ServiceLoader.java:789) ~[na:na]
... 53 common frames omitted
Caused by: java.lang.NoClassDefFoundError: net/mamoe/mirai/internal/deps/io/ktor/client/engine/okhttp/OkHttp
at net.mamoe.mirai.internal.MiraiImplKt.createDefaultHttpClient(MiraiImpl.kt:29) ~[mirai-core-jvm-2.13.0.jar:2.13.0]
at net.mamoe.mirai.internal.MiraiImpl.<init>(MiraiImpl.kt:97) ~[mirai-core-jvm-2.13.0.jar:2.13.0]
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) ~[na:na]
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) ~[na:na]
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480) ~[na:na]
at java.base/java.util.ServiceLoader$ProviderImpl.newInstance(ServiceLoader.java:789) ~[na:na]
... 72 common frames omitted
Caused by: java.lang.ClassNotFoundException: net.mamoe.mirai.internal.deps.io.ktor.client.engine.okhttp.OkHttp
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641) ~[na:na]
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188) ~[na:na]
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520) ~[na:na]
... 80 common frames omitted
进程已结束,退出代码1
我添加了 okhttp 的依赖项也无法解决 pom文件:
<properties>
<mirai.version>2.13.0</mirai.version>
</properties>
<dependency>
<groupId>net.mamoe</groupId>
<artifactId>mirai-core-jvm</artifactId>
<version>${mirai.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.8.1</version>
<scope>compile</scope>
</dependency>
我不知道如何解决 Okhttp 的依赖冲突(如果问题是这个的话,因为看包名net/mamoe/mirai/internal/deps/io/ktor/client/engine/okhttp/OkHttp似乎是在mirai自己内部实现了,不应该外部管理?)