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 java.net.URLClassLoader.findClass(URLClassLoader.java:382) ~[?:1.8.0_201] at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[?:1.8.0_201] at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[?:1.8.0_201] at java.lang.ClassLoader.defineClass1(Native Method) ~[?:1.8.0_201] at java.lang.ClassLoader.defineClass(ClassLoader.java:763) ~[?:1.8.0_201] at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) ~[?:1.8.0_201] at java.net.URLClassLoader.defineClass(URLClassLoader.java:468) ~[?:1.8.0_201] at java.net.URLClassLoader.access$100(URLClassLoader.java:74) ~[?:1.8.0_201] at java.net.URLClassLoader$1.run(URLClassLoader.java:369) ~[?:1.8.0_201] at java.net.URLClassLoader$1.run(URLClassLoader.java:363) ~[?:1.8.0_201] at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_201] at java.net.URLClassLoader.findClass(URLClassLoader.java:362) ~[?:1.8.0_201] at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[?:1.8.0_201] at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[?:1.8.0_201] at net.raynight.addons.AddonManager.loadAddons(AddonManager.java:18) ~[?:?] at net.raynight.command.TestCommand.onCommand(TestCommand.java:17) ~[?:?] at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-21fe707-e1ebe52] ... 8 more

    Code:
    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) {
                e.printStackTrace();
                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.