Solved How can i load listener from .class file?

Lexize

New member
Feb 4, 2022
11
1
0
1
15
Hello guys. Im newbie at java, and i really have no idea why i getting that error.
Code:
[15:29:32 WARN]: Unexpected exception while parsing console command "event setClass Parkour"
org.bukkit.command.CommandException: Unhandled exception executing command 'event' in plugin Demorgan v1.2-SNAPSHOT
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[paper-api-1.18.1-R0.1-SNAPSHOT.jar:?]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:159) ~[paper-api-1.18.1-R0.1-SNAPSHOT.jar:?]
        at org.bukkit.craftbukkit.v1_18_R1.CraftServer.dispatchCommand(CraftServer.java:897) ~[paper-1.18.1.jar:git-Paper-133]
        at org.bukkit.craftbukkit.v1_18_R1.CraftServer.dispatchServerCommand(CraftServer.java:860) ~[paper-1.18.1.jar:git-Paper-133]
        at net.minecraft.server.dedicated.DedicatedServer.handleConsoleInputs(DedicatedServer.java:507) ~[paper-1.18.1.jar:git-Paper-133]
        at net.minecraft.server.dedicated.DedicatedServer.tickChildren(DedicatedServer.java:481) ~[paper-1.18.1.jar:git-Paper-133]
        at net.minecraft.server.MinecraftServer.tickServer(MinecraftServer.java:1470) ~[paper-1.18.1.jar:git-Paper-133]
        at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:1264) ~[paper-1.18.1.jar:git-Paper-133]
        at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:317) ~[paper-1.18.1.jar:git-Paper-133]
        at java.lang.Thread.run(Thread.java:833) ~[?:?]
Caused by: java.lang.NoClassDefFoundError: org/bukkit/event/Listener
        at java.lang.ClassLoader.defineClass1(Native Method) ~[?:?]
        at java.lang.ClassLoader.defineClass(ClassLoader.java:1012) ~[?:?]
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150) ~[?:?]
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:524) ~[?:?]
        at java.net.URLClassLoader$1.run(URLClassLoader.java:427) ~[?:?]
        at java.net.URLClassLoader$1.run(URLClassLoader.java:421) ~[?:?]
        at java.security.AccessController.doPrivileged(AccessController.java:712) ~[?:?]
        at java.net.URLClassLoader.findClass(URLClassLoader.java:420) ~[?:?]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:587) ~[?:?]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:520) ~[?:?]
        at java.lang.Class.forName0(Native Method) ~[?:?]
        at java.lang.Class.forName(Class.java:467) ~[?:?]
        at org.lexize.demorgan.EventManager.SetListener(EventManager.java:60) ~[Demorgan-1.2-SNAPSHOT.jar:?]
        at org.lexize.demorgan.Commands.EventCommand.onCommand(EventCommand.java:65) ~[Demorgan-1.2-SNAPSHOT.jar:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[paper-api-1.18.1-R0.1-SNAPSHOT.jar:?]
        ... 9 more
Caused by: java.lang.ClassNotFoundException: org.bukkit.event.Listener
        at java.net.URLClassLoader.findClass(URLClassLoader.java:445) ~[?:?]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:587) ~[?:?]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:520) ~[?:?]
        at java.lang.ClassLoader.defineClass1(Native Method) ~[?:?]
        at java.lang.ClassLoader.defineClass(ClassLoader.java:1012) ~[?:?]
        at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:150) ~[?:?]
        at java.net.URLClassLoader.defineClass(URLClassLoader.java:524) ~[?:?]
        at java.net.URLClassLoader$1.run(URLClassLoader.java:427) ~[?:?]
        at java.net.URLClassLoader$1.run(URLClassLoader.java:421) ~[?:?]
        at java.security.AccessController.doPrivileged(AccessController.java:712) ~[?:?]
        at java.net.URLClassLoader.findClass(URLClassLoader.java:420) ~[?:?]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:587) ~[?:?]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:520) ~[?:?]
        at java.lang.Class.forName0(Native Method) ~[?:?]
        at java.lang.Class.forName(Class.java:467) ~[?:?]
        at org.lexize.demorgan.EventManager.SetListener(EventManager.java:60) ~[Demorgan-1.2-SNAPSHOT.jar:?]
        at org.lexize.demorgan.Commands.EventCommand.onCommand(EventCommand.java:65) ~[Demorgan-1.2-SNAPSHOT.jar:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[paper-api-1.18.1-R0.1-SNAPSHOT.jar:?]
        ... 9 more

Here is my code of method "SetListener":
Java:
    public static void SetListener(String path, String listenerClassName) throws MalformedURLException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        URL url = new File(path).toURI().toURL();
        URL[] urls = new URL[]{url};
        ClassLoader cl = new URLClassLoader(urls, ClassLoader.getSystemClassLoader());
        Class listener = Class.forName(listenerClassName, true, cl);
        HandlerList.unregisterAll(currentListener);
        currentListener = (Listener) listener.getDeclaredConstructor().newInstance();
        Bukkit.getPluginManager().registerEvents(currentListener, Demorgan.instance);
    }

Really need help. Thank you!
 

jmp

Paper Maintainer
Staff member
Dec 11, 2021
6
3
3
The reason it can't see Bukkit classes is that you're using the wrong parent ClassLoader.
 

ryantheleach

New member
Jan 23, 2022
8
2
3
I assume they are trying to make a modular plugin, that only loads the module's it's configured to load, or load remote classes.

I assume this, because it's the only reason you would need to mess with attempting to load classes in a bukkit plugin, outside some sort of dynamic reload or skript.

If you are doing this because you assume you have to, in order to have your listeners inside another jar file, or assuming you have to do this in order to have your listeners inside another class, this is false.

For Jar Files you can set those up as their own plugins, and use plugin.yml to set up dependencies between them.

for other classes, @LoJoSho has shown how to do it, assuming the class is on the classpath via some sane build tooling like gradle/maven.
 
  • Like
Reactions: LoJoSho

LoJoSho

New member
Jan 4, 2022
12
4
3
I am confused about what are you doing...

Typically, this is show you handle registering a listener:

Main.java:
Java:
public final class Main extends JavaPlugin {
   
    @Override
    public void onEnable() {
        this.getServer().getPluginManager().registerEvents(new PlayerPunch(), this);
    }
}

Replace "PlayerPunch" with the other class you are referencing. But you would be still editing in a .java file.

If this still doesn't help, what exactly are you trying to do? // Why are you trying to load a .class file?
 

Lexize

New member
Feb 4, 2022
11
1
0
1
15
Oh, sorry guys. I already solved my problem, but i forget to write about it there. Sorry.
About how do i solve my problem
I just make my custom class with OnCustomEvent() method.