java.lang.NoSuchMethodException

Discussion in 'Plugin Development' started by TNOMCat, Jul 23, 2011.

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

    TNOMCat

    Getting this error when i start my plugin. Everything seems to be OK in the class, also plugin.yml is okay. How can i fix this?
    Code:
    21:58:50 [SEVERE] Could not load 'plugins\Creeper.jar' in folder 'plugins':
    java.lang.NoSuchMethodException: me.TheNoobOfMystery.Creeper.Creeper.<init>()
            at java.lang.Class.getConstructor0(Unknown Source)
            at java.lang.Class.getConstructor(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:171)
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.
    java:207)
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager
    .java:130)
            at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:128)
            at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:97)
            at net.minecraft.server.ServerConfigurationManager.<init>(ServerConfigur
    ationManager.java:51)
            at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:132)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:335)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:422)
     
  2. Offline

    bleachisback

    Paste the main class
     
  3. Offline

    Mixcoatl

    This exception says you're missing a zero-argument (default) constructor.
     
  4. Offline

    bleachisback

    No, the most likely problem is that he is missing onEnable(). He could also have typed the package name out wrong
     
  5. Offline

    Mixcoatl

    The failure is occurring in getConstructor. I'm not certain you can legally define a JavaPlugin subclass without an onEnable method, but if you can, it would not raise this specific error.
     
  6. Offline

    TNOMCat

    I dont think im missing anything, and i have onEnable.
    Main class:
    Code:
    package me.TheNoobOfMystery.Creeper;
    
    import java.util.logging.Logger;
    
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
    
    class Creeper extends JavaPlugin {
    
        Logger log = Logger.getLogger("Minecraft");
    
        public void onEnable(){
             log.info("[Creeper] Creeper v1.00 is enabled.");
        }
    
        public void onDisable(){
             log.info("[Creeper] Creeper v1.00 is disabled.");
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(cmd.getName().equalsIgnoreCase("creep")){
                log.info("lol");
                return true;
            }
            return false;
        }
    
    }
    
     
  7. Offline

    Mixcoatl

    You're missing a zero-argument (default) constructor. Add this:
    Code:
    public Creeper() {
      // Do nothing.
    }
     
  8. Offline

    TNOMCat

    Thanks! :D Its working now.
     
  9. Offline

    Mixcoatl

    Java doesn't require you to create a default constructor, as it knows how to default-construct the class (although there are a few cases where it requires you to provide a constructor.) Unfortunately, this functionality is not exposed through the reflection API, so when Bukkit requests the default constructor to create the plug-in instance, it fails. This is the exception you were seeing.
     
  10. Offline

    TNOMCat

    Wait, its still not working.
    Now i get this error:

    Code:
    23:30:38 [SEVERE] Could not load 'plugins\Creeper.jar' in folder 'plugins':
    java.lang.IllegalAccessException: Class org.bukkit.plugin.java.JavaPluginLoader
    can not access a member of class me.TheNoobOfMystery.Creeper.Creeper with modifi
    ers "public"
            at sun.reflect.Reflection.ensureMemberAccess(Unknown Source)
            at java.lang.reflect.Constructor.newInstance(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.j
    ava:173)
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.
    java:207)
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager
    .java:130)
            at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:128)
            at org.bukkit.craftbukkit.CraftServer.<init>(CraftServer.java:97)
            at net.minecraft.server.ServerConfigurationManager.<init>(ServerConfigur
    ationManager.java:51)
            at net.minecraft.server.MinecraftServer.init(MinecraftServer.java:132)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:335)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:422)
    
     
  11. Offline

    Mixcoatl

    Please make certain you declared the constructor "public".
     
  12. Offline

    TNOMCat

    Yea, i did.
     
  13. Offline

    bleachisback

    His class isn't public, make it
    Code:
    public class Creeper
    You don't need a constructor =3=
     
  14. Offline

    Mixcoatl

    Good catch!
     
  15. Offline

    TNOMCat

    The class is public.

    EDIT: Wait, no it wasnt :D
     
  16. Offline

    bleachisback

    Then what do you call this:
    Code:
    class Creeper extends JavaPlugin {
    I don't see any public anywhere
     
  17. Offline

    TNOMCat

    Yeah i just noticed it. lol

    Now it works :D thanks again

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 17, 2016
  18. I just wonder why it is not implemented to create plugins with a public getter method. After all plugins are more or less singletons. Would this be just overkill or anancastic?
    Just for fun I created one and obviously it didn't get loaded, spitting out the error mentioned above, then I found this thread.
    Code:
    class MySingleton extends JavaPlugin(){
     
    static MySingleton Instance;
     
    private MySingleton(){}
     
    public MySingleton getInstance(){
        if (Instance==null){
        return MySingleton Instance = new MySingleton();
        }
        return Instance;
     
    }
    
     
  19. Offline

    fireblast709

    Because static modifiers create even more memory leaks. Bukkit already has enough of those I heard
     
  20. Is this a property of the java garbage collection being too lazy or how does that come?
     
Thread Status:
Not open for further replies.

Share This Page