NoClassDefFoundException while trying to use HikariCP [duplicate]

孤街醉人 提交于 2019-12-12 03:57:15

问题


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


回答1:


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?




回答2:


From the exception it is clear that HikariCP-2.6.0.jar was in classpath during compile time but is missing in runtime and from the image of the project structure, it is also clear that both HikariCP-2.6.0.jar and slf4j-api-1.7.21.jar are missing as library reference in the ide. You need to keep these jar in your classpath library during compile time and runtime.



来源:https://stackoverflow.com/questions/41908170/noclassdeffoundexception-while-trying-to-use-hikaricp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!