Solved Getting NoClassDefFoundError when trying to include my own library in a Plugin.

Discussion in 'Spigot Plugin Development' started by maxbridgland, Mar 6, 2020.

  1. For some reason I can't use a custom External Library I'm trying to build I keep getting this error:

    Code (Text):
    java.lang.NoClassDefFoundError: com/maxbridgland/easylocaleapi/EasyLocaleAPI
            at com.maxbridgland.eltest.ELTPlugin.onEnable(ELTPlugin.java:18) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) ~[spigot.jar:git-Spigot-8faa8b4-fba9f48]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:351) [spigot.jar:git-Spigot-8faa8b4-fba9f48]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:432) [spigot.jar:git-Spigot-8faa8b4-fba9f48]
            at org.bukkit.craftbukkit.v1_15_R1.CraftServer.enablePlugin(CraftServer.java:464) [spigot.jar:git-Spigot-8faa8b4-fba9f48]
            at org.bukkit.craftbukkit.v1_15_R1.CraftServer.enablePlugins(CraftServer.java:378) [spigot.jar:git-Spigot-8faa8b4-fba9f48]
            at net.minecraft.server.v1_15_R1.MinecraftServer.a(MinecraftServer.java:457) [spigot.jar:git-Spigot-8faa8b4-fba9f48]
            at net.minecraft.server.v1_15_R1.DedicatedServer.init(DedicatedServer.java:274) [spigot.jar:git-Spigot-8faa8b4-fba9f48]
            at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:784) [spigot.jar:git-Spigot-8faa8b4-fba9f48]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_221]
    Caused by: java.lang.ClassNotFoundException: com.maxbridgland.easylocaleapi.EasyLocaleAPI
            at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_221]
            at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:167) ~[spigot.jar:git-Spigot-8faa8b4-fba9f48]
            at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:96) ~[spigot.jar:git-Spigot-8faa8b4-fba9f48]
            at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_221]
            at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_221]
            ... 10 more
    My plugin is not using maven but my library is. When i open up the Referenced Libraries in Eclipse I see that there are my classes. However I still cannot get it to run correctly.

    My Code:

    Library:

    EasyLocaleAPI.java
    Code (Java):
    package com.maxbridgland.easylocaleapi;

    import org.bukkit.plugin.java.JavaPlugin;

    public class EasyLocaleAPI {

        public static ELLocalizer getLocalizer(JavaPlugin plugin, String defaultLocale){
            return new ELLocalizer(plugin, defaultLocale);
        }
    }

     
    ELLocale.java
    Code (Java):
    package com.maxbridgland.easylocaleapi;

    import java.util.HashMap;

    public class ELLocale {

        String localeName = null;
        HashMap<String, String> localeMap = new HashMap<String, String>();

        public void setLocaleName(String localeName) {
            this.localeName = localeName;
        }

        public void setLocalMap(HashMap<String, String> localeMap) {
            this.localeMap = localeMap;
        }

        public String getLocaleName() {
            return localeName;
        }

        public HashMap<String, String> getLocaleMap() {
            return localeMap;
        }

        public boolean isLocalKey(String key) {
            return localeMap.containsKey(key);
        }

        public String getTranslation(String key) {
            if (localeMap != null && localeMap.containsKey(key)) {
                return localeMap.get(key);
            }
            return null;
        }

        public int size() {
            return localeMap.size();
        }

    }
    ELLocalizer.java

    Code (Java):
    package com.maxbridgland.easylocaleapi;

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map.Entry;

    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    import com.google.gson.Gson;

    public class ELLocalizer {

        public String defaultLocale;
        public JavaPlugin plugin;
        public HashMap<String, String> defaultLocaleMap = new HashMap<String, String>();
        public File localeFolder;
        public HashMap<String, ELLocale> localeMap = new HashMap<String, ELLocale>();

        public ELLocalizer(JavaPlugin plugin, String defaultLocale) {
            this.plugin = plugin;
            Gson gson = new Gson();
            localeFolder = new File(plugin.getDataFolder() + File.pathSeparator + "locales" + File.pathSeparator);
            if (localeFolder.exists()) {
                this.defaultLocale = defaultLocale;
                if (localeFolder.listFiles() != null) {
                    if (localeFolder.listFiles().length > 0) {
                        for (File path : localeFolder.listFiles()) {
                            if (path.getName().endsWith(".json")) {
                                if (path.getName().replace(".json", "").equalsIgnoreCase(defaultLocale)) {
                                    try {
                                        BufferedReader bf = new BufferedReader(new FileReader(plugin.getDataFolder() + "locales/" + path));
                                        ELLocale lMap = gson.fromJson(bf, ELLocale.class);
                                        lMap.setLocaleName(path.getName().replace(".json", ""));
                                        if (lMap != null && lMap.size() > 0) {
                                            for (Entry<String, String> ent : lMap.getLocaleMap().entrySet()) {
                                                defaultLocaleMap.put(ent.getKey(), ent.getValue());
                                            }
                                        }
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                                try {
                                    BufferedReader bf = new BufferedReader(new FileReader(plugin.getDataFolder() + File.pathSeparator + "locales" + File.pathSeparator + path));
                                    ELLocale lMap = gson.fromJson(bf, ELLocale.class);
                                    lMap.setLocaleName(path.getName().replace(".json", ""));
                                    if (lMap != null && lMap.size() > 0) {
                                        localeMap.put(lMap.getLocaleName(), lMap);
                                    }
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                }
            }
        }

        public String getDefaultLocale() {
            return defaultLocale;
        }

        public HashMap<String, ELLocale> getAvailableLocales(){
            return localeMap;
        }

        public File getLocaleFolder() {
            return localeFolder;
        }

        public boolean localeFolderExists() {
            return localeFolder.exists();
        }

        public String translateToLocale(String key, Player player) {
            String locale = player.getLocale().toLowerCase();
            if (defaultLocaleMap.containsKey(key)) {
                if (localeMap.containsKey(locale)) {
                    if (localeMap.get(locale).getLocaleMap().containsKey(key)) {
                        return localeMap.get(locale).getTranslation(key);
                    }
                }
                return defaultLocaleMap.get(key);
            }
            return "Missing Locale For This Key: " + key;
        }

    }
     
    Test Plugin:

    ELTest.java


    Code (Java):
    package com.maxbridgland.eltest;

    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    import com.maxbridgland.easylocaleapi.ELLocalizer;
    import com.maxbridgland.easylocaleapi.EasyLocaleAPI;

    public class ELTPlugin extends JavaPlugin implements Listener {
       
        ELLocalizer localizer;
       
        @Override
        public void onEnable() {
            this.getServer().getPluginManager().registerEvents(this, this);
            localizer = EasyLocaleAPI.getLocalizer(this, "en_us");
        }
       
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
            String msg = localizer.translateToLocale("welcome", e.getPlayer());
            e.getPlayer().sendMessage(msg);
        }
       
    }
     
    Any help would be great, thank you!
     
  2. You have to shade your library into your plugin.
     
  3. Any decent guides on this? I'm having issues finding help for libraries for Spigot
     
  4. Just google: maven shade dependencies
    It's a very common thing