NoClassDefFoundException while trying to use HikariCP

SrLegsini picture SrLegsini · Jan 28, 2017 · Viewed 7.3k times · Source

I'm so noob at external stuff to Bukkit programming, so I'm sorry if it's so easy to solve :P

I have a problem, and it's that when I try to use HikariCP in my project, it returns in an error (the title one).

I'm using it in a BungeeCord plugin. The weird thing is that I have done this successfully couples of times, and I don't know why it isn't working this time.

The error / log:

06:13:36 [ADVERTENCIA] Exception encountered when loading plugin: DiverseReport java.lang.NoClassDefFoundError: com/zaxxer/hikari/HikariDataSource at net.srlegsini.DiverseReport.Bungee.MClass.onEnable(MClass.java:44) at net.md_5.bungee.api.plugin.PluginManager.enablePlugins(PluginManager.java:227) at net.md_5.bungee.BungeeCord.start(BungeeCord.java:272) at net.md_5.bungee.BungeeCordLauncher.main(BungeeCordLauncher.java:55) at net.md_5.bungee.Bootstrap.main(Bootstrap.java:15) Caused by: java.lang.ClassNotFoundException: com.zaxxer.hikari.HikariDataSource at net.md_5.bungee.api.plugin.PluginClassloader.loadClass0(PluginClassloader.java:53) at net.md_5.bungee.api.plugin.PluginClassloader.loadClass(PluginClassloader.java:27) at java.lang.ClassLoader.loadClass(Unknown Source) ... 5 more

My main class:

package net.srlegsini.DiverseReport.Bungee;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;

import com.zaxxer.hikari.HikariDataSource;

import net.md_5.bungee.BungeeCord;
import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.config.Configuration;
import net.md_5.bungee.config.ConfigurationProvider;
import net.md_5.bungee.config.YamlConfiguration;
import net.srlegsini.DiverseReport.Bukkit.UUIDFetcher;

public class MClass extends Plugin {

    static Configuration config;
    static MClass plugin;

    static HikariDataSource hikari;
    static Connection connection;

    public void onEnable() {
        BungeeCord.getInstance().getPluginManager().registerListener(this, new ChannelListener());
        BungeeCord.getInstance().registerChannel("Return");

        loadCfg();

        if (!config.contains("MySQL")) {
            config.set("MySQL.Enable", false);

            config.set("MySQL.Host", "localhost");
            config.set("MySQL.Port", 3306);
            config.set("MySQL.User", "user");
            config.set("MySQL.Pass", "pass");
            config.set("MySQL.Database", "Sr_DiverseReport");
        }

        saveCfg(getDataFolder());

        hikari = new HikariDataSource();
        hikari.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
        hikari.addDataSourceProperty("serverName", config.getString("MySQL.Host"));
        hikari.addDataSourceProperty("port", 3306);
        hikari.addDataSourceProperty("databaseName", config.getString("MySQL.Database"));
        hikari.addDataSourceProperty("user", config.getString("MySQL.User"));
        hikari.addDataSourceProperty("password", config.getString("MySQL.Pass"));

        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = hikari.getConnection();
        } catch (SQLException e1) {
            e1.printStackTrace();
        } catch (ClassNotFoundException e2) {

        }

        saveCfg(getDataFolder());



    }

    public void loadCfg() {
        try {
            File file = new File(getDataFolder(), "config.yml");
            if (!getDataFolder().exists()) {
                getDataFolder().mkdir();
            }

            if (!file.exists()) {
                file.createNewFile();
            }
            config = ConfigurationProvider.getProvider(YamlConfiguration.class)
                    .load(new File(getDataFolder(), "config.yml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void saveCfg(File dataFolder) {
        try {
            ConfigurationProvider.getProvider(YamlConfiguration.class).save(config, new File(dataFolder, "config.yml"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @SuppressWarnings({ "unused", "deprecation" })
    public static String getUUID(String playerName) {
        UUIDFetcher fetcher = new UUIDFetcher(Arrays.asList("evilmidget38", "mbaxter"));
        String playerUUID = null;
        try {
            playerUUID = UUIDFetcher.getUUIDOf(playerName).toString();
        } catch (Exception e2) {
            playerUUID = BungeeCord.getInstance().getPlayer(playerName).getUniqueId().toString();
        }

        return playerUUID;
    }

}

My procedure: Create the project, import BungeeCord.jar, HikariCP-2.6.0.jar and slf4j-api-1.7.21.jar in buildpath, import HikariCP-2.6.0.jar and slf4j-api-1.7.21.jar

It worked in other projects, but magically, it's broken. I don't want to use Maven, just because it must have a fix, because as I said, I used this same procedure so many times in the past.

Thank you for taking the time to read this :)

EDIT: Image of the project

Answer

jediz picture jediz · Jan 28, 2017

It's all in the exception:

Caused by: java.lang.ClassNotFoundException: com.zaxxer.hikari.HikariDataSource

The HikariDataSource is missing at runtime, you need to provide it somehow, for example by copying the relevant .jar with 'drivers' into your server libraries folder.

Also see some related questions: How to set up datasource with Spring for HikariCP? and How do I configure HikariCP in my Spring Boot app in my application.properties files?