在RunMirai上运行正常,编译成插件运行时就抛出ClassNotFoundException
-
用Hutool的CronUtil写了一个定时任务,CronUtil用法如下
Hutool添加全局定时任务-CronUtil
配置文件内容如下:[org.ASN70.apiExercise] #每5秒向控制台打印当前时间(测试用) getAPI.jobs.Test = */5 * * * * * *
定时任务类代码如下:
class getAPI { //需要定时执行的任务 object jobs { fun Test() { val nowIs = LocalDateTimeUtil.now().format(DateTimeFormatter.ofPattern("HH-mm-ss")).toString() println(nowIs) } } }
在RunMirai下运行没有任何问题,可以每5秒向控制台打印时间,但是编译成插件在Console运行时(Mirai Console 2.12.0)有如下报错
2022-07-31 11:32:13 E/api功能练习插件: cn.hutool.cron.CronException: Schedule [*/5 * * * * * *] [org.ASN70.apiExercise.getAPI.jobs.Test] error! cn.hutool.cron.CronException: Schedule [*/5 * * * * * *] [org.ASN70.apiExercise.getAPI.jobs.Test] error! at mirai-console-ASN70-apiExercise-0.1.0.mirai2.jar[private]//cn.hutool.cron.Scheduler.schedule(Scheduler.java:216) at mirai-console-ASN70-apiExercise-0.1.0.mirai2.jar[private]//cn.hutool.cron.CronUtil.schedule(CronUtil.java:106) at mirai-console-ASN70-apiExercise-0.1.0.mirai2.jar[private]//cn.hutool.cron.CronUtil.start(CronUtil.java:170) at mirai-console-ASN70-apiExercise-0.1.0.mirai2.jar[private]//cn.hutool.cron.CronUtil.start(CronUtil.java:143) at mirai-console-ASN70-apiExercise-0.1.0.mirai2.jar//org.ASN70.apiExercise.ApiExerciseMain.onEnable(ApiExerciseMain.kt:65) at net.mamoe.mirai.console.internal.plugin.JvmPluginInternal.internalOnEnable$mirai_console(JvmPluginInternal.kt:131) at net.mamoe.mirai.console.internal.plugin.BuiltInJvmPluginLoaderImpl.enable(BuiltInJvmPluginLoaderImpl.kt:278) at net.mamoe.mirai.console.internal.plugin.BuiltInJvmPluginLoaderImpl.enable(BuiltInJvmPluginLoaderImpl.kt:35) at net.mamoe.mirai.console.plugin.jvm.JvmPluginLoader$BuiltIn.enable(JvmPluginLoader.kt) at net.mamoe.mirai.console.plugin.jvm.JvmPluginLoader$BuiltIn.enable(JvmPluginLoader.kt:50) at net.mamoe.mirai.console.plugin.PluginManager.enablePlugin(PluginManager.kt:173) at net.mamoe.mirai.console.internal.plugin.PluginManagerImpl.enableAllLoadedPlugins$mirai_console(PluginManagerImpl.kt:178) at net.mamoe.mirai.console.internal.MiraiConsoleImplementationBridge.doStart$mirai_console(MiraiConsoleImplementationBridge.kt:235) at net.mamoe.mirai.console.MiraiConsoleImplementation$Companion.start(MiraiConsoleImplementation.kt:464) at net.mamoe.mirai.console.terminal.MiraiConsoleTerminalLoader.startAsDaemon(MiraiConsoleTerminalLoader.kt:168) at net.mamoe.mirai.console.terminal.MiraiConsoleTerminalLoader.startAsDaemon$default(MiraiConsoleTerminalLoader.kt:167) at net.mamoe.mirai.console.terminal.MiraiConsoleTerminalLoader.main(MiraiConsoleTerminalLoader.kt:52) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.base/java.lang.reflect.Method.invoke(Unknown Source) at org.itxtech.mcl.Utility.bootJars(Utility.java:89) at org.itxtech.mcl.Utility.bootJars(Utility.java:79) at org.itxtech.mcl.Utility.bootMirai(Utility.java:101) at org.itxtech.mcl.module.builtin.Boot.boot(Boot.java:109) at org.itxtech.mcl.module.ModuleManager.phaseBoot(ModuleManager.java:148) at org.itxtech.mcl.Loader.lambda$start$4(Loader.java:189) at org.itxtech.mcl.Loader.tryCatching(Loader.java:146) at org.itxtech.mcl.Loader.start(Loader.java:189) at org.itxtech.mcl.Loader.main(Loader.java:79) Caused by: cn.hutool.core.exceptions.UtilException: ClassNotFoundException: org.ASN70.apiExercise.getAPI.jobs at mirai-console-ASN70-apiExercise-0.1.0.mirai2.jar[private]//cn.hutool.core.util.ClassLoaderUtil.doLoadClass(ClassLoaderUtil.java:319) at mirai-console-ASN70-apiExercise-0.1.0.mirai2.jar[private]//cn.hutool.core.util.ClassLoaderUtil.lambda$loadClass$1(ClassLoaderUtil.java:202) at mirai-console-ASN70-apiExercise-0.1.0.mirai2.jar[private]//cn.hutool.core.map.ReferenceConcurrentMap.lambda$computeIfAbsent$1(ReferenceConcurrentMap.java:137) at java.base/java.util.concurrent.ConcurrentHashMap.computeIfAbsent(Unknown Source) at mirai-console-ASN70-apiExercise-0.1.0.mirai2.jar[private]//cn.hutool.core.map.ReferenceConcurrentMap.computeIfAbsent(ReferenceConcurrentMap.java:137) at mirai-console-ASN70-apiExercise-0.1.0.mirai2.jar[private]//cn.hutool.core.util.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:202) at mirai-console-ASN70-apiExercise-0.1.0.mirai2.jar[private]//cn.hutool.core.util.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:167) at mirai-console-ASN70-apiExercise-0.1.0.mirai2.jar[private]//cn.hutool.core.util.ClassLoaderUtil.loadClass(ClassLoaderUtil.java:148) at mirai-console-ASN70-apiExercise-0.1.0.mirai2.jar[private]//cn.hutool.cron.task.InvokeTask.<init>(InvokeTask.java:43) at mirai-console-ASN70-apiExercise-0.1.0.mirai2.jar[private]//cn.hutool.cron.Scheduler.schedule(Scheduler.java:214) ... 29 more Caused by: java.lang.ClassNotFoundException: org.ASN70.apiExercise.getAPI.jobs at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source) at java.base/java.lang.ClassLoader.loadClass(Unknown Source) at java.base/java.lang.Class.forName0(Native Method) at java.base/java.lang.Class.forName(Unknown Source) at mirai-console-ASN70-apiExercise-0.1.0.mirai2.jar[private]//cn.hutool.core.util.ClassLoaderUtil.doLoadClass(ClassLoaderUtil.java:314) ... 38 more
-
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
mirai-console (2.12.0)插件 的 class 不会加载在 BuiltinClassLoader ,而是每个插件有独立的 ClassLoader
比较简单的处理方法是
var oc = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader()); // do something } finally { Thread.currentThread().setContextClassLoader(oc); }
-
@cssxsh 感谢解答,这部分代码该怎么使用呢,是要放在哪里?
-
@N70_
这段代码是用来包裹
CronUtil.schedule
的你应该把
CronUtil.schedule
放在// do something
那里 -
@cssxsh 在PluginMain里只用到了这三个方法
CronUtil.setCronSetting("$configFolderPath\\cron.setting") CronUtil.setMatchSecond(true) CronUtil.start()
我应该去CronUtil.java源文件里添加您给出的这段代码吗
-
都不知道怎么说你好
那就改成你用的这三句阿var oc = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader()); CronUtil.setCronSetting("$configFolderPath/cron.setting") CronUtil.setMatchSecond(true) CronUtil.start() } finally { Thread.currentThread().setContextClassLoader(oc); }
-
@cssxsh 太感激您了!!已经解决问题了!!