ClassDefNotFoundError

Discussion in 'Plugin Development' started by Numenorean95, Oct 29, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Numenorean95

    When trying to run my plugin, i get this:
    Code:
    161 recipes
    17 achievements
    18:03:52 [INFO] Starting minecraft server version Beta 1.8.1
    18:03:52 [INFO] Loading properties
    18:03:52 [INFO] Starting Minecraft server on *:25566
    18:03:52 [INFO] This server is running Craftbukkit version git-Bukkit-0.0.0-1112
    -g54a13b2-b1240jnks (MC: 1.8.1)
    18:03:52 [INFO] Preparing level "world"
    18:03:52 [INFO] Default game type: 0
    18:03:53 [INFO] Preparing start region for level 0 (Seed: 1076970115706439648)
    18:03:53 [INFO] Preparing start region for level 1 (Seed: -7402833689365332090)
    18:03:53 [INFO] [StrategyCraft] Loading Buildings...
    18:03:53 [INFO] [StrategyCraft] Found building Citadel.jar, attempting to load..
    .
    18:03:53 [SEVERE] Error occurred while enabling StrategyCraft v0.0.5 (Is it up t
    o date?): net/lotrcraft/strategycraft/buildings/Building
    java.lang.NoClassDefFoundError: net/lotrcraft/strategycraft/buildings/Building
            at java.lang.ClassLoader.defineClass1(Native Method)
            at java.lang.ClassLoader.defineClassCond(Unknown Source)
            at java.lang.ClassLoader.defineClass(Unknown Source)
            at java.security.SecureClassLoader.defineClass(Unknown Source)
            at java.net.URLClassLoader.defineClass(Unknown Source)
            at java.net.URLClassLoader.access$000(Unknown Source)
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at net.lotrcraft.strategycraft.loader.BuildingLoader.loadBuilding(Buildi
    ngLoader.java:41)
            at net.lotrcraft.strategycraft.Config.load(Config.java:49)
            at net.lotrcraft.strategycraft.Main.onEnable(Main.java:33)
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:126)
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:941)
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:280)
            at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:174)
            at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:157
    )
            at net.minecraft.server.MinecraftServer.e(MinecraftServer.java:297)
            at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:284)
            at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:152)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:348)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:417)
    Caused by: java.lang.ClassNotFoundException: net.lotrcraft.strategycraft.buildin
    gs.Building
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            ... 24 more
    18:03:53 [INFO] Server permissions file permissions.yml is empty, ignoring it
    18:03:53 [INFO] Done (0.098s)! For help, type "help" or "?"
    >
    My plugin is somewhat unusual, as it requires external jars that extend a class within the main plugin (kindof like bukkit and bukkit plugins). Whenever i try to run it, it throws a ClassDefNotFoundError. I have insured that the jar is not sealed, and am at a complete loss how to procede. Any suggestions?
    EDIT: Link to plugin in signature, top one.
     
  2. Any chance of seeing your building loader class as that's where the error is coming from :)
     
  3. Offline

    Numenorean95

    Doh, should have thought of that. Still though, i dont believe that the problem is in my code, per se.
    The loader:
    Code:
                d = new BuildingDescription(name,
                        new URLClassLoader(
                                new URL[] { file.toURI().toURL() })
                                    .loadClass(building) //The error occurs exactly here. "building" is a class path in string form.
                                        .asSubclass(Building.class),
                        new URLClassLoader(
                                new URL[] { file.toURI().toURL() }).loadClass(unit)
                                .asSubclass(Unit.class));
    An example Building:
    Code:
    package net.lotrcraft.scbuildings;
    
    import net.lotrcraft.strategycraft.buildings.Building;
    import net.lotrcraft.strategycraft.schematic.Schematic;
     
    public class Citadel extends Building {
    
    //...
    }
    It should be noted that if i package the base plugin into the building jar it seems to work but causes a different problem.

    If i pakage the main source into the building:
    Code:
    161 recipes
    17 achievements
    19:45:59 [INFO] Starting minecraft server version Beta 1.8.1
    19:45:59 [INFO] Loading properties
    19:45:59 [INFO] Starting Minecraft server on *:25566
    19:45:59 [INFO] This server is running Craftbukkit version git-Bukkit-0.0.0-1112
    -g54a13b2-b1240jnks (MC: 1.8.1)
    19:45:59 [INFO] Preparing level "world"
    19:45:59 [INFO] Default game type: 0
    19:45:59 [INFO] Preparing start region for level 0 (Seed: 1076970115706439648)
    19:45:59 [INFO] Preparing start region for level 1 (Seed: -7402833689365332090)
    19:46:00 [INFO] [StrategyCraft] Loading Buildings...
    19:46:00 [INFO] [StrategyCraft] Found building Citadel.jar, attempting to load..
    .
    19:46:00 [SEVERE] Error occurred while enabling StrategyCraft v0.0.5 (Is it up t
    o date?): class net.lotrcraft.scbuildings.Citadel
    java.lang.ClassCastException: class net.lotrcraft.scbuildings.Citadel
            at java.lang.Class.asSubclass(Unknown Source)
            at net.lotrcraft.strategycraft.loader.BuildingLoader.loadBuilding(Buildi
    ngLoader.java:42)
            at net.lotrcraft.strategycraft.Config.load(Config.java:49)
            at net.lotrcraft.strategycraft.Main.onEnable(Main.java:33)
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:126)
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader
    .java:941)
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManage
    r.java:280)
            at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:174)
            at org.bukkit.craftbukkit.CraftServer.enablePlugins(CraftServer.java:157
    )
            at net.minecraft.server.MinecraftServer.e(MinecraftServer.java:297)
            at net.minecraft.server.MinecraftServer.a(MinecraftServer.java:284)
            at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:152)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:348)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:417)
    19:46:00 [INFO] Server permissions file permissions.yml is empty, ignoring it
    19:46:00 [INFO] Done (0.086s)! For help, type "help" or "?"
    >
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  4. Well the problem seems to be it cannot find the class hmmm can I see an example class path? If it works in the jar but not outside I think I may know the problem.
    EDIT: Just saw the new error :p I may be able to solve the original error but you will still get this error as it appears to be trying to cast the class.

    Upon further reading into the error that specific error (when it can actually find the class in the same jar) is because the building (Citadel.class etc.) cannot be cast as a subclass of Building.class

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  5. Offline

    Numenorean95

    well, i can try and fix that later, but i still need to fix the first one. An example class path would be just net.lotrcraft.strategycraft.Main, or the such.
    I tried meerly casting it, but i am having some trouble with it. Im afraid i do not know much about class loading, and google doesnt help.
     
  6. I may have found the problem and if it is then it was staring us right in the face :p try removing the (extends building) in your building classes (citadel etc.) as subclass automatically makes it an extension so your trying to extend it twice. This is just a wild guess but try it! As for it not finding the class make sure the 'file' variable is pointing to the correct jar. Without seeing your complete source code I can only guess but I'm not gonna ask to because that takes the fun away :p
     
  7. Offline

    nisovin

    You need to pass the main class loader into the constructor of your own URLClassLoader.

    Code:
    d = new BuildingDescription(name, new URLClassLoader(new URL[] { file.toURI().toURL() }, getClassLoader()) .....
     
    Numenorean95 likes this.
  8. Offline

    Numenorean95

    Haha! You are brilliant, never would have guessed that. The first error is now fixed, and i have to fix the casting error.

    Oh, fine, ill send you the code ;) Im not sure exactly what you want though. The problem is, i want to have the external jar extend Building. I tried this:
    Code:
                d = new BuildingDescription(name,
                        (Class<? extends Building>) new URLClassLoader(new URL[] { file.toURI().toURL() }, Main.class.getClassLoader())
                                .loadClass(building).cast(Building.class),
                        (Class<? extends Unit>) new URLClassLoader(new URL[] { file.toURI().toURL() }, Main.class.getClassLoader()).loadClass(unit)
                                .cast(Unit.class));
    It still doesnt work... Any other ideas?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 20, 2016
  9. Did you try removing the 'extends' from your citadel class? And the other building classes if that works. Subclassing is the same as extending.
     
  10. Offline

    nisovin

    Is Main the name of your JavaPlugin class?
     
  11. Offline

    Numenorean95

    Yes, that part works. The error is on the next line now, a ClassCastException error
    //Im having trouble copying the log, will add tommorrow.
     
  12. Offline

    nisovin

    Why did you change it from asSubclass()? I'd try something like this:
    Code:
    URLClassLoader ucl = new URLClassLoader(new URL[] { file.toURI().toURL() }, Main.class.getClassLoader());
    d = new BuildingDescription(name, Class.forName(building, true, ucl).asSubclass(Building.class), Class.forName(unit, true, ucl).asSubclass(Unit.class));
    
     
  13. Offline

    Numenorean95

    Fixed! Thanks a hundred times over, i have no idea why it didnt work in the first place.
     
Thread Status:
Not open for further replies.

Share This Page