SIGAR api throwing NoClassDefFoundError

256 Views Asked by At

I'm using the latest IntelliJ IDEA , and the SIGAR.jar has been added to my project using the external library option from menu. Although everything seems fine from IDE , it showing the error when i try to run the plugin. The error which gets printed out is : java.lang.NoClassDefFoundError: org/hyperic/sigar/SigarException

I've already tried to check my code , plus I've tried to check my imports an they're supposedly done correctly. I haven't found any proof of why my code should not work.

This is my Lscpu class which is implementing some of the SIGAR methods. The IDE is not showing up any error on this code:

public class Lscpu implements CommandExecutor {
    Runtime serverRunTime = Runtime.getRuntime();

    private void getCpuTotal() {
        Sigar sigar = new Sigar();
        CpuInfo[] infos;
        try {
            infos = sigar.getCpuInfoList();
            for (int k = 0; k < infos.length; k++) {
                CpuInfo info = infos[k];
                System.out.println(Utility.ANSI_YELLOW_BACKGROUND + "--------------------------" + Utility.ANSI_RESET);
                System.out.println(Utility.ANSI_YELLOW_BACKGROUND + "[CPU Clock-Speed]: " + info.getMhz() + Utility.ANSI_RESET);
                System.out.println(Utility.ANSI_YELLOW_BACKGROUND + "[CPU Brand-Name]: " + info.getVendor() + Utility.ANSI_RESET);
                System.out.println(Utility.ANSI_YELLOW_BACKGROUND + "[CPU Model-Name]: " + info.getModel() + Utility.ANSI_RESET);
                System.out.println(Utility.ANSI_YELLOW_BACKGROUND + "[CPU Cache-Size]: " + info.getCacheSize() + Utility.ANSI_RESET);
                System.out.println(Utility.ANSI_YELLOW_BACKGROUND + "[CPU Cores-number]:" + info.getTotalCores() + Utility.ANSI_RESET);
            }
        } catch (SigarException exc) {
            exc.printStackTrace();
        }
    }

    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String s, String[] args) {
        if (sender instanceof Player) {
            Player player = (Player) sender;
            if (args.length == 0) {
                sender.sendMessage(Utility.chat("&7«« &fSystem Info &7»»"));
                getCpuTotal();
                sender.sendMessage(Utility.chat("&7 Go lookup on your console, the information have been displayed there."));
            } else {
                sender.sendMessage(Utility.chat("&4»» &7The command syntax is incorrect!"));
            }
        } else if (args.length == 0) {
            getCpuTotal();
        } else {
            System.out.println(Utility.ANSI_YELLOW_BACKGROUND + Utility.ANSI_RED + "The command syntax you used is incorrect, use /lscpu or its aliases." + Utility.ANSI_RESET);
        }
        return false;
    }
}

I expected the console to not give the error on boot and when the user inserts the /lscpu command from either console or in-game he prints out the cpu info provided by the SIGAR's api. This is not working and the complete error is this:

    at me.thevipershow.memoryinfo.MemoryInfo.onEnable(MemoryInfo.java:14) ~[?:?]
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:264) ~[spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:337) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:403) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugin(CraftServer.java:381) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    at org.bukkit.craftbukkit.v1_12_R1.CraftServer.enablePlugins(CraftServer.java:330) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    at net.minecraft.server.v1_12_R1.MinecraftServer.t(MinecraftServer.java:422) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    at net.minecraft.server.v1_12_R1.MinecraftServer.l(MinecraftServer.java:383) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    at net.minecraft.server.v1_12_R1.MinecraftServer.a(MinecraftServer.java:338) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    at net.minecraft.server.v1_12_R1.DedicatedServer.init(DedicatedServer.java:272) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:545) [spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_222]
Caused by: java.lang.ClassNotFoundException: org.hyperic.sigar.SigarException
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382) ~[?:1.8.0_222]
    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:152) ~[spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:100) ~[spigot-1.12.2.jar:git-Spigot-79a30d7-acbc348]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[?:1.8.0_222]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[?:1.8.0_222]
    ... 12 more```
1

There are 1 best solutions below

1
Joseph Sible-Reinstate Monica On

You're adding the jar just in the IDE, so it isn't making it into your mod, which is built with Gradle. You need to either edit your build.gradle to "shade" the jar, or add the jar to Minecraft's classpath.