samedi 22 avril 2017

Java Reflections failing to get classes that implement an interface

I am sorry if this is a dumb question, but it has been really bothering me for two days and I am trying to figure it out.

I am trying to get all classes that implement an interface named ICommand using the Reflections library. Then, my method for returning the classes returns a Set<? extends ICommand> which I later use a for loop on in another class (Help) and use getDescription() and getCommandName() on each class.

I've tried a few times with some different codes but I am currently getting the exception java.lang.NoClassDefFoundError caused by java.lang.ClassNotFoundException. Keep in mind that the problem occurs at creating a new Reflection instance with the classpath provided inside of the CommandTools class.

I don't know what is causing these problems, I think everything is alright but could there be a problem with the classpath or am I missing something?

Finally, the code:

ICommand Interface

public interface ICommand {

    public void action(MessageReceivedEvent event);

    public String getDescription();

    public String getCommandName();

    public String getUsage();

}

CommandTools class with returnCommands() method

import java.util.Set;

import org.reflections.Reflections;

import com.FatCat.memecat.Core.ICommand;

public class CommandTools {

    public static Set<Class<? extends ICommand>> returnCommands() {
        Reflections reflections = new Reflections("com.FatCat.memecat.Commands");

        return reflections.getSubTypesOf(ICommand.class);
    }
}

A part of the code from the Help class that implements ICommand

    String commandList = "";

    for (Object c : CommandTools.returnCommands().toArray()) {
        commandList = commandList + "`+" + ((ICommand) c).getCommandName() + "` ➤ "
                + ((ICommand) c).getDescription() + "" + "\n";
    }

    event.getTextChannel()
            .sendMessage(Messaging.eBuild(event, "Commands", commandList, ColorList.standard, true))
            .queue((m) -> {
                Messaging.reactions(reactionInfo, 500, m);
            });

Error log

[23:18:07] [Fatal] [JDA]: java.lang.NoClassDefFoundError: com/google/common/base/Predicate
    at com.FatCat.memecat.Functions.CommandTools.returnCommands(CommandTools.java:12)
    at com.FatCat.memecat.Commands.Informative.Help.action(Help.java:24)
    at com.FatCat.memecat.Listeners.CommandListener.onMessageReceived(CommandListener.java:38)
    at net.dv8tion.jda.core.hooks.ListenerAdapter.onEvent(ListenerAdapter.java:329)
    at net.dv8tion.jda.core.hooks.InterfacedEventManager.handle(InterfacedEventManager.java:84)
    at net.dv8tion.jda.core.handle.MessageCreateHandler.handleDefaultMessage(MessageCreateHandler.java:129)
    at net.dv8tion.jda.core.handle.MessageCreateHandler.handleInternally(MessageCreateHandler.java:50)
    at net.dv8tion.jda.core.handle.SocketHandler.handle(SocketHandler.java:38)
    at net.dv8tion.jda.core.requests.WebSocketClient.handleEvent(WebSocketClient.java:722)
    at net.dv8tion.jda.core.requests.WebSocketClient.onTextMessage(WebSocketClient.java:459)
    at com.neovisionaries.ws.client.ListenerManager.callOnTextMessage(ListenerManager.java:352)
    at com.neovisionaries.ws.client.ReadingThread.callOnTextMessage(ReadingThread.java:262)
    at com.neovisionaries.ws.client.ReadingThread.callOnTextMessage(ReadingThread.java:240)
    at com.neovisionaries.ws.client.ReadingThread.handleTextFrame(ReadingThread.java:965)
    at com.neovisionaries.ws.client.ReadingThread.handleFrame(ReadingThread.java:748)
    at com.neovisionaries.ws.client.ReadingThread.main(ReadingThread.java:110)
    at com.neovisionaries.ws.client.ReadingThread.run(ReadingThread.java:66)
Caused by: java.lang.ClassNotFoundException: com.google.common.base.Predicate
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 17 more





Aucun commentaire:

Enregistrer un commentaire