Solved Addons

Discussion in 'Spigot Plugin Development' started by RayNightStudios, Jul 14, 2019.

Thread Status:
Not open for further replies.
  1. So I wanna create a AddonManager that loads Addons from a folder where the main class of the jar extends an Addon class. I got the code but it prints this error.

    Caused by: java.lang.ClassNotFoundException: net.raynight.addons.Addon at ~[?:1.8.0_201] at java.lang.ClassLoader.loadClass( ~[?:1.8.0_201] at java.lang.ClassLoader.loadClass( ~[?:1.8.0_201] at java.lang.ClassLoader.defineClass1(Native Method) ~[?:1.8.0_201] at java.lang.ClassLoader.defineClass( ~[?:1.8.0_201] at ~[?:1.8.0_201] at ~[?:1.8.0_201] at$100( ~[?:1.8.0_201] at$ ~[?:1.8.0_201] at$ ~[?:1.8.0_201] at Method) ~[?:1.8.0_201] at ~[?:1.8.0_201] at java.lang.ClassLoader.loadClass( ~[?:1.8.0_201] at java.lang.ClassLoader.loadClass( ~[?:1.8.0_201] at net.raynight.addons.AddonManager.loadAddons( ~[?:?] at net.raynight.command.TestCommand.onCommand( ~[?:?] at org.bukkit.command.PluginCommand.execute( ~[spigot.jar:git-Spigot-21fe707-e1ebe52] ... 8 more

    Code (Text):
            for (File file : new File("plugins/RayNight/Addons/").listFiles()) {
                URL url = file.toURL();
                URL[] urls = new URL[]{url};
                ClassLoader cl = new URLClassLoader(urls);
                Class cls = cl.loadClass("net.raynight.Main");
                if(Addon.class.isAssignableFrom(cls)) {
                    Addon addon = (Addon) cls.newInstance();
    it looks like it cant find my plugin how can I fix it so that the jar which is loaded knows the Addon class?
  2. Some time ago i was messing with this as well, but i found it over complicated and unnecesary so i drop it, but here is the code:

    Code (Java):
        public Class<?> getModule(File file) {
            try (JarFile jarFile = new JarFile(file)) {
                Enumeration<JarEntry> en = jarFile.entries();
                URLClassLoader child = new URLClassLoader(new URL[] { file.toURI().toURL() }, getClass().getClassLoader());
                while (en.hasMoreElements()) {
                    JarEntry next = en.nextElement();
                    String name = next.getName();
                    if (name.endsWith(".class")) {
                        name = name.replace('/', '.');
                        Class<?> clazz = Class.forName(name.substring(0, name.length() - 6), true, child); // We remove the ".class" from the name
                        if (Addon.class.isAssignableFrom(clazz))
                            return clazz;
            } catch (IOException | ClassNotFoundException e) {
                return null;
            return null;
  3. Thank you! I will try it :D
  4. It worked. Thank you so much!
  5. yw, close it and mark it as solved :B
Thread Status:
Not open for further replies.