project-mirai / mirai-console-plugin-template

Template for Mirai Console plugins
GNU Affero General Public License v3.0
67 stars 17 forks source link

关于java实例 #5

Closed Nambers closed 3 years ago

Nambers commented 3 years ago

官方有java sdk吗? 没找到然后尝试自己写了个,然后主类继承的构造函数好像出问题了,能不能写个java部分或帮我看看怎么改? 谢谢了,估计是主类没静态

package org.example.mirai.plugin;

import net.mamoe.mirai.console.plugin.jvm.JavaPlugin;
import net.mamoe.mirai.console.plugin.jvm.JvmPluginDescriptionBuilder;
import net.mamoe.mirai.event.GlobalEventChannel;
import net.mamoe.mirai.event.events.GroupMessageEvent;

public final class JavaPluginMain extends JavaPlugin {
    public JavaPluginMain() {
        super(new JvmPluginDescriptionBuilder("id","version").build());
    }
//    super(new JvmPluginDescriptionBuilder(
//                "org.example.mirai-example",
//                        "0.1.0").build());

    @Override
    public void onEnable() {

        getLogger().info("hhhhhh");
        GlobalEventChannel.INSTANCE.subscribeAlways(GroupMessageEvent.class, groupMessageEvent -> {
            getLogger().info(groupMessageEvent.getMessage().contentToString());
        });
    }
}
Him188 commented 3 years ago

然后主类继承的构造函数好像出问题了

是什么问题?

Nambers commented 3 years ago

mcl报错

2021-02-04 23:16:14 E/main: Failed to init MiraiConsole.
net.mamoe.mirai.console.internal.util.ServiceLoadException: Could not load service org.example.mirai.plugin.JavaPluginMain.JavaPluginMain.
        at net.mamoe.mirai.console.internal.util.PluginServiceHelper.loadService(PluginServiceHelper.kt:57)
        at net.mamoe.mirai.console.internal.util.PluginServiceHelper.loadAllServices(PluginServiceHelper.kt:39)
        at net.mamoe.mirai.console.internal.plugin.BuiltInJvmPluginLoaderImpl$extractPlugins$1$2.invoke(BuiltInJvmPluginLoaderImpl.kt:75)
        at net.mamoe.mirai.console.internal.plugin.BuiltInJvmPluginLoaderImpl$extractPlugins$1$2.invoke(BuiltInJvmPluginLoaderImpl.kt:29)
        at kotlin.sequences.TransformingSequence$iterator$1.next(Sequences.kt:210)
        at kotlin.sequences.FlatteningSequence$iterator$1.ensureItemIterator(Sequences.kt:314)
        at kotlin.sequences.FlatteningSequence$iterator$1.hasNext(Sequences.kt:303)
        at kotlin.sequences.TransformingSequence$iterator$1.hasNext(Sequences.kt:214)
        at kotlin.sequences.TransformingSequence$iterator$1.hasNext(Sequences.kt:214)
        at kotlin.sequences.FlatteningSequence$iterator$1.ensureItemIterator(Sequences.kt:316)
        at kotlin.sequences.FlatteningSequence$iterator$1.hasNext(Sequences.kt:303)
        at kotlin.sequences.SequencesKt___SequencesKt.toCollection(_Sequences.kt:752)
        at kotlin.sequences.SequencesKt___SequencesKt.toSet(_Sequences.kt:793)
        at net.mamoe.mirai.console.internal.plugin.BuiltInJvmPluginLoaderImpl.extractPlugins(BuiltInJvmPluginLoaderImpl.kt:94)
        at net.mamoe.mirai.console.plugin.loader.AbstractFilePluginLoader.listPlugins(FilePluginLoader.kt:51)
        at net.mamoe.mirai.console.plugin.jvm.JvmPluginLoader$BuiltIn.listPlugins(JvmPluginLoader.kt)
        at net.mamoe.mirai.console.internal.plugin.PluginManagerImpl.listAndSortAllPlugins(PluginManagerImpl.kt:186)
        at net.mamoe.mirai.console.internal.plugin.PluginManagerImpl.findAndSortAllPluginsUsingBuiltInLoaders(PluginManagerImpl.kt:130)
        at net.mamoe.mirai.console.internal.plugin.PluginManagerImpl.loadAllPluginsUsingBuiltInLoaders$mirai_console(PluginManagerImpl.kt:140)
        at net.mamoe.mirai.console.internal.MiraiConsoleImplementationBridge.doStart$mirai_console(MiraiConsoleImplementationBridge.kt:152)
        at net.mamoe.mirai.console.MiraiConsoleImplementation$Companion.start(MiraiConsoleImplementation.kt:209)
        at net.mamoe.mirai.console.terminal.MiraiConsoleTerminalLoader.startAsDaemon(MiraiConsoleTerminalLoader.kt:153)
        at net.mamoe.mirai.console.terminal.MiraiConsoleTerminalLoader.startAsDaemon$default(MiraiConsoleTerminalLoader.kt:152)
        at net.mamoe.mirai.console.terminal.MiraiConsoleTerminalLoader.main(MiraiConsoleTerminalLoader.kt:48)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:564)
        at org.itxtech.mcl.Utility.bootMirai(Utility.java:84)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:564)
        at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:138)
        at org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:226)
        at org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:1692)
        at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:1013)
        at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:109)
        at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:412)
        at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3578)
        at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:107)
        at org.mozilla.javascript.ArrowFunction.call(ArrowFunction.java:43)
        at org.mozilla.javascript.InterfaceAdapter.invokeImpl(InterfaceAdapter.java:155)
        at org.mozilla.javascript.InterfaceAdapter.lambda$invoke$0(InterfaceAdapter.java:105)
        at org.mozilla.javascript.Context.call(Context.java:554)
        at org.mozilla.javascript.ContextFactory.call(ContextFactory.java:522)
        at org.mozilla.javascript.InterfaceAdapter.invoke(InterfaceAdapter.java:105)
        at org.mozilla.javascript.jdk18.VMBridge_jdk18$1.invoke(VMBridge_jdk18.java:126)
        at com.sun.proxy.$Proxy4.run(Unknown Source)
        at org.itxtech.mcl.script.ScriptManager.phaseBoot(ScriptManager.java:101)
        at org.itxtech.mcl.Loader.start(Loader.java:128)
        at org.itxtech.mcl.Loader.main(Loader.java:53)
Caused by: java.lang.ClassNotFoundException: org.example.mirai.plugin.JavaPluginMain.JavaPluginMain
        at net.mamoe.mirai.console.internal.plugin.JvmPluginClassLoader.findClass$mirai_console(JvmPluginClassLoader.kt:110)
        at net.mamoe.mirai.console.internal.plugin.JvmPluginClassLoader.findClass(JvmPluginClassLoader.kt:51)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
        at java.base/java.lang.Class.forName0(Native Method)
        at java.base/java.lang.Class.forName(Class.java:468)
        at net.mamoe.mirai.console.internal.util.PluginServiceHelper.loadService(PluginServiceHelper.kt:48)
        ... 51 more
Him188 commented 3 years ago

service 配置错误, 应该是 org.example.mirai.plugin.JavaPluginMain

Him188 commented 3 years ago

请安装 Mirai Console Intellij 插件, 这个错误会有自动修复

Nambers commented 3 years ago

okok 我待会看看

Nambers commented 3 years ago

service 配置错误, 应该是 org.example.mirai.plugin.JavaPluginMain

这个我知道,改过了,你看那个报错信息里面就是javapluginmain

Him188 commented 3 years ago

报错是 org.example.mirai.plugin.JavaPluginMain.JavaPluginMain 正确是 org.example.mirai.plugin.JavaPluginMain

Nambers commented 3 years ago

啊 那个是我当时加一个试一下的忘记改回去了,下面是报错

2021-02-05 13:03:38 E/main: Failed to init MiraiConsole.
net.mamoe.mirai.console.internal.util.ServiceLoadException: Could not load service org.example.mirai.plugin.JavaPluginMain.
        at net.mamoe.mirai.console.internal.util.PluginServiceHelper.loadService(PluginServiceHelper.kt:57)
        at net.mamoe.mirai.console.internal.util.PluginServiceHelper.loadAllServices(PluginServiceHelper.kt:39)
        at net.mamoe.mirai.console.internal.plugin.BuiltInJvmPluginLoaderImpl$extractPlugins$1$2.invoke(BuiltInJvmPluginLoaderImpl.kt:75)
        at net.mamoe.mirai.console.internal.plugin.BuiltInJvmPluginLoaderImpl$extractPlugins$1$2.invoke(BuiltInJvmPluginLoaderImpl.kt:29)
        at kotlin.sequences.TransformingSequence$iterator$1.next(Sequences.kt:210)
        at kotlin.sequences.FlatteningSequence$iterator$1.ensureItemIterator(Sequences.kt:314)
        at kotlin.sequences.FlatteningSequence$iterator$1.hasNext(Sequences.kt:303)
        at kotlin.sequences.TransformingSequence$iterator$1.hasNext(Sequences.kt:214)
        at kotlin.sequences.TransformingSequence$iterator$1.hasNext(Sequences.kt:214)
        at kotlin.sequences.FlatteningSequence$iterator$1.ensureItemIterator(Sequences.kt:316)
        at kotlin.sequences.FlatteningSequence$iterator$1.hasNext(Sequences.kt:303)
        at kotlin.sequences.SequencesKt___SequencesKt.toCollection(_Sequences.kt:752)
        at kotlin.sequences.SequencesKt___SequencesKt.toSet(_Sequences.kt:793)
        at net.mamoe.mirai.console.internal.plugin.BuiltInJvmPluginLoaderImpl.extractPlugins(BuiltInJvmPluginLoaderImpl.kt:94)
        at net.mamoe.mirai.console.plugin.loader.AbstractFilePluginLoader.listPlugins(FilePluginLoader.kt:51)
        at net.mamoe.mirai.console.plugin.jvm.JvmPluginLoader$BuiltIn.listPlugins(JvmPluginLoader.kt)
        at net.mamoe.mirai.console.internal.plugin.PluginManagerImpl.listAndSortAllPlugins(PluginManagerImpl.kt:186)
        at net.mamoe.mirai.console.internal.plugin.PluginManagerImpl.findAndSortAllPluginsUsingBuiltInLoaders(PluginManagerImpl.kt:130)
        at net.mamoe.mirai.console.internal.plugin.PluginManagerImpl.loadAllPluginsUsingBuiltInLoaders$mirai_console(PluginManagerImpl.kt:140)
        at net.mamoe.mirai.console.internal.MiraiConsoleImplementationBridge.doStart$mirai_console(MiraiConsoleImplementationBridge.kt:152)
        at net.mamoe.mirai.console.MiraiConsoleImplementation$Companion.start(MiraiConsoleImplementation.kt:209)
        at net.mamoe.mirai.console.terminal.MiraiConsoleTerminalLoader.startAsDaemon(MiraiConsoleTerminalLoader.kt:153)
        at net.mamoe.mirai.console.terminal.MiraiConsoleTerminalLoader.startAsDaemon$default(MiraiConsoleTerminalLoader.kt:152)
        at net.mamoe.mirai.console.terminal.MiraiConsoleTerminalLoader.main(MiraiConsoleTerminalLoader.kt:48)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:564)
        at org.itxtech.mcl.Utility.bootMirai(Utility.java:84)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:564)
        at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:138)
        at org.mozilla.javascript.NativeJavaMethod.call(NativeJavaMethod.java:226)
        at org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:1692)
        at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:1013)
        at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:109)
        at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:412)
        at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3578)
        at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:107)
        at org.mozilla.javascript.ArrowFunction.call(ArrowFunction.java:43)
        at org.mozilla.javascript.InterfaceAdapter.invokeImpl(InterfaceAdapter.java:155)
        at org.mozilla.javascript.InterfaceAdapter.lambda$invoke$0(InterfaceAdapter.java:105)
        at org.mozilla.javascript.Context.call(Context.java:554)
        at org.mozilla.javascript.ContextFactory.call(ContextFactory.java:522)
        at org.mozilla.javascript.InterfaceAdapter.invoke(InterfaceAdapter.java:105)
        at org.mozilla.javascript.jdk18.VMBridge_jdk18$1.invoke(VMBridge_jdk18.java:126)
        at com.sun.proxy.$Proxy4.run(Unknown Source)
        at org.itxtech.mcl.script.ScriptManager.phaseBoot(ScriptManager.java:101)
        at org.itxtech.mcl.Loader.start(Loader.java:128)
        at org.itxtech.mcl.Loader.main(Loader.java:53)
Caused by: java.lang.ClassNotFoundException: org.example.mirai.plugin.JavaPluginMain
        at net.mamoe.mirai.console.internal.plugin.JvmPluginClassLoader.findClass$mirai_console(JvmPluginClassLoader.kt:110)
        at net.mamoe.mirai.console.internal.plugin.JvmPluginClassLoader.findClass(JvmPluginClassLoader.kt:51)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:589)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
        at java.base/java.lang.Class.forName0(Native Method)
        at java.base/java.lang.Class.forName(Class.java:468)
        at net.mamoe.mirai.console.internal.util.PluginServiceHelper.loadService(PluginServiceHelper.kt:48)
        ... 51 more
Him188 commented 3 years ago

检查Java编译目标版本, 使用 mirai-console gradle 插件打包

Nambers commented 3 years ago

检查Java编译目标版本, 使用 mirai-console gradle 插件打包

用[jar]和[buildPlugin]都打包过了,都不可用,我用现在这个配置打包kt的作为主类都是可以运行的,我觉得有2种可能, 1 是java主类的继承和入口点配置出问题,所以来问下 2 是java的主类不能用kt的gradle配置? 目前我的gradle配置是

plugins {
    val kotlinVersion = "1.4.21"
    kotlin("jvm") version kotlinVersion
    kotlin("plugin.serialization") version kotlinVersion
    id("net.mamoe.mirai-console") version "2.3.2"
}

group = "org.example"
version = "0.1.0"

repositories {
    jcenter()
    //国内镜像源
    //华为云
    maven { url =uri("https://mirrors.huaweicloud.com/repository/maven") }
    //阿里云
    maven { url =uri("https://maven.aliyun.com/nexus/content/repositories/jcenter")}
    //mirai-console的仓库
    maven {url = uri("https://dl.bintray.com/him188moe/mirai")}
    //滑动模块的仓库
    // maven { url = uri("https://dl.bintray.com/karlatemp/misc") }
    mavenLocal()
    mavenCentral()
}
dependencies{
    //在IDE内运行的mcl添加滑块模块,请参考https://github.com/project-mirai/mirai-login-solver-selenium把版本更新为最新
    //runtimeOnly("net.mamoe:mirai-login-solver-selenium:1.0-dev-15")
}
Him188 commented 3 years ago

Kotlin编译目标默认为6或8, Java默认目标是你JDK的版本. 有可能你启动时用的 JRE 环境版本比你JDK低

Nambers commented 3 years ago

idea的java版本是1.8,电脑环境是openjdk15

Nambers commented 3 years ago

用runmiraikt运行没问题,且在同一个项目里用kt的做主类可以运行在mcl里

Nambers commented 3 years ago

IDEA的java版本改到open jdk 11 还是报错,内容不变,我认为是入口点和继承那些的问题,可能是静态问题因为kt里主类是object

Him188 commented 3 years ago

java 文件放在了 src/main/kotlin? Kotlin 兼容 Java 但 Java 不兼容 Kotlin

Nambers commented 3 years ago

java 文件放在了 src/main/kotlin? Kotlin 兼容 Java 但 Java 不兼容 Kotlin

确实是这个问题hhhh,之前没试过kt+java混合在一个项目里