Solved Cannot get plugin for class from a static initializer

Discussion in 'Spigot Plugin Development' started by thecamzone, Oct 27, 2019.

  1. Hey everyone,
    When trying to enable my plugin, I get thrown an exception that I'm having a hard time figuring out. As far as I can see there is nothing that is initializing the class as static, I may be just misinterpreting the error.
    Thanks for any help!

    Here is the full error:
    Code (Text):
    [17:11:26] [Server thread/ERROR]: Could not load 'plugins\Admin.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.IllegalStateException: Cannot get plugin for class me.TheCamZone.main.Main from a static initializer
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:139) ~[spigot.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.plugin.SimplePluginManager.loadPlugin(SimplePluginManager.java:334) ~[spigot.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.plugin.SimplePluginManager.loadPlugins(SimplePluginManager.java:253) [spigot.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.craftbukkit.v1_14_R1.CraftServer.loadPlugins(CraftServer.java:350) [spigot.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.craftbukkit.v1_14_R1.CraftServer.reload(CraftServer.java:808) [spigot.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.Bukkit.reload(Bukkit.java:576) [spigot.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:27) [spigot.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) [spigot.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.craftbukkit.v1_14_R1.CraftServer.dispatchCommand(CraftServer.java:710) [spigot.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.craftbukkit.v1_14_R1.CraftServer.dispatchServerCommand(CraftServer.java:695) [spigot.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.DedicatedServer.handleCommandQueue(DedicatedServer.java:433) [spigot.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.DedicatedServer.b(DedicatedServer.java:397) [spigot.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.a(MinecraftServer.java:971) [spigot.jar:git-Spigot-9de398a-9c887d4]
            at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:816) [spigot.jar:git-Spigot-9de398a-9c887d4]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_221]
    Caused by: java.lang.IllegalStateException: Cannot get plugin for class me.TheCamZone.main.Main from a static initializer
            at org.bukkit.plugin.java.JavaPlugin.getPlugin(JavaPlugin.java:395) ~[spigot.jar:git-Spigot-9de398a-9c887d4]
            at me.TheCamZone.main.ConfigManager.<init>(ConfigManager.java:14) ~[?:?]
            at me.TheCamZone.main.Commands.<init>(Commands.java:14) ~[?:?]
            at me.TheCamZone.main.Main.<init>(Main.java:12) ~[?:?]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_221]
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_221]
            at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[?:1.8.0_221]
            at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[?:1.8.0_221]
            at java.lang.Class.newInstance(Unknown Source) ~[?:1.8.0_221]
            at org.bukkit.plugin.java.PluginClassLoader.<init>(PluginClassLoader.java:71) ~[spigot.jar:git-Spigot-9de398a-9c887d4]
            at org.bukkit.plugin.java.JavaPluginLoader.loadPlugin(JavaPluginLoader.java:135) ~[spigot.jar:git-Spigot-9de398a-9c887d4]
            ... 14 more

    Here is my Main Class:
    Code (Java):
    package me.TheCamZone.main;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    import me.TheCamZone.main.Events.EventClass;
    import net.md_5.bungee.api.ChatColor;
    public class Main extends JavaPlugin implements Listener {
        private ConfigManager cfgm;
        public Commands commands = new Commands();
     
        @Override
        public void onEnable()
        {
            loadConfigManager();
       
            getCommand(commands.cmd1).setExecutor(commands);
            getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "Admin Plugin Has Been Enabled.");
            getServer().getPluginManager().registerEvents(new EventClass(), this);
        }
     
        @Override
        public void onDisable()
        {
            getServer().getConsoleSender().sendMessage(ChatColor.RED + "Admin Plugin Has Been Disabled.");
        }
     
        public void loadConfigManager()
        {
            cfgm = new ConfigManager();
            cfgm.setup();
            cfgm.saveVanishedPlayers();
            cfgm.reloadVanishedPlayers();
        }
     
        public void loadConfig()
        {
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
     
    }

    And here is my ConfigManager file. This is the only place I reference Main outside of itself.
    Code (Java):
    package me.TheCamZone.main;
    import java.io.File;
    import java.io.IOException;
    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import net.md_5.bungee.api.ChatColor;
    public class ConfigManager {
        private Main plugin = Main.getPlugin(Main.class);
     
        public FileConfiguration vanishedplayerscfg;
        public File vanishplayersfile;
     
        public void setup()
        {
            if(!plugin.getDataFolder().exists())
            {
                plugin.getDataFolder().mkdir();
            }
       
            vanishplayersfile = new File(plugin.getDataFolder(), "vanishedplayers.yml");
       
            if(!vanishplayersfile.exists()) {
                try {
                    vanishplayersfile.createNewFile();
                    Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "vanishedplayers.yml file has been created.");
                } catch(IOException e) {
                    Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.DARK_RED + "Could not create the vanishedplayers.yml file");
                }
            }
       
            vanishedplayerscfg = YamlConfiguration.loadConfiguration(vanishplayersfile);
        }
     
        public FileConfiguration getVanishedPlayers()
        {
            return vanishedplayerscfg;
        }
     
        public void saveVanishedPlayers()
        {
            try {
                Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "players.yml has been saved");
                vanishedplayerscfg.save(vanishplayersfile);
            } catch(IOException e) {
                Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.RED + "Could not save the vanishedplayers.yml file");
            }
        }
     
        public void reloadVanishedPlayers()
        {
            vanishedplayerscfg = YamlConfiguration.loadConfiguration(vanishplayersfile);
            Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "vanishedplayers.yml has been reloaded");
        }
    }

    The error did not start until I called the main method in ConfigManager.
    I followed a tutorial for setting up multiple configuration files as I did not have experience in that yet, the code worked in the tutorial video.

    EDIT: I have looked through a couple of posts with the same question and they all seem to actually have a "static" tag somewhere in their initialization.
     
    #1 thecamzone, Oct 27, 2019
    Last edited: Oct 28, 2019
  2. You should really learn proper OOP.
    First of all, you have loadConfig and never use that method.
    You should create a constructor and you do not initialize your main class that way

    public MainClass clazz;
    className(plugin instance) {
    clazz = instance;
    }
    Avoid naming your main class Main as well
     
  3. Ah, I see, the constructor fixed it, thank you for your help!