Search code examples
javaclassloaderclassnotfoundexceptionurlclassloader

Can't find a class using Class.forName()


This is my "test-addon"

-

And I'm trying to load my "main class" using:

Class<?> jarClass;
try {
    ClassLoader cls = ClassLoader.getSystemClassLoader();
    jarClass = cls.loadClass("main.Addon");
} catch (ClassNotFoundException e) {
    throw new InvalidPluginException("\"Addon class\" was not found", e);
}

As you can see in the image, the class exists, but it still returns: enter image description here

Line 21: jarClass = cls.loadClass("main.Addon");

QUESTION: why does this happen


Solution

  • The problem was that I used the SystemClassLoader, not my own.

    A simple fix for this was: jarClass = Class.forName("main.Addon", true, this);