Search code examples
javamavenminecrafthikaricp

NoClassDefFoundError after importing hikaricp through Maven dependency


I am getting a java.lang.NoClassDefFoundError: com/zaxxer/hikari/HikariDataSource error when launching my Java plugin on spigot. from what I can tell I have correctly imported it.

I know this question has been posted multiple times but by looking through 3-4 I do not see any clear answer to what is wrong. The code crashes at hikari = new HikariDataSource() which is also the first hikari statement used.

My Pom

<groupId>drhampust.github.io</groupId>
<artifactId>Blank</artifactId>
<version>1.0-SNAPSHOT</version>

<repositories>
    <repository>
        <id>spigot-repo</id>
        <url>https://hub.spigotmc.org/nexus/content/repositories/snapshot/</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>org.spigotmc</groupId>
        <artifactId>spigot-api</artifactId>
        <version>1.14.4-R0.1-SNAPSHOT</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.17</version>
    </dependency>
    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
        <version>3.4.0</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

it seems like hikari does not compile into jar but I can use its assets whilst coding.

Code:

import com.zaxxer.hikari.HikariDataSource;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;

import java.sql.*;

    public class Main extends JavaPlugin {

    //DataBase vars.
    public String username, password, database, host, properties, table; //db variables
    public int port; //db variable

    //Connection vars
    private HikariDataSource hikari;

    @Override
    public void onEnable() {
        getLogger().info("onEnable is called!");
        loadConfig();

        new BukkitRunnable(){
            @Override
            public void run(){
                connectToDatabase();
            }
        }.runTaskAsynchronously(this);

        this.getServer().getPluginManager().registerEvents(new SetterGetter(), this);
    }
    @Override
    public void onDisable() {
        getLogger().info("onDisable is called!");
    }


    public synchronized void connectToDatabase() {
        //Database details
        String address = getConfig().getString("Database.address");
        String name = getConfig().getString("Database.Name");
        String username = getConfig().getString("Database.username");
        String password = getConfig().getString("Database.password");
        int port = getConfig().getInt("Database.port");

        //Initialise hikari instace
        hikari = new HikariDataSource();

        //Setting Hikari properties
        hikari.setMaximumPoolSize(10);
        hikari.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
        hikari.addDataSourceProperty("serverName", address);
        hikari.addDataSourceProperty("port", port);
        hikari.addDataSourceProperty("databaseName", name);
        hikari.addDataSourceProperty("user", username);
        hikari.addDataSourceProperty("password", password);
    }

    public void loadConfig() {
        getConfig().options().copyDefaults(true);
        saveConfig();
    }
}

full crash report in a paste bin to make it easier to see: https://pastebin.com/JEMz0f6T

My attempt to create a standalone program using hikaricp: Code:

package test;

import com.zaxxer.hikari.HikariDataSource;

public class Main
{
    public static void main(String[] args) {
        HikariDataSource hikari = new HikariDataSource();

        //Setting Hikari properties
        hikari.setMaximumPoolSize(10);
        hikari.setDriverClassName("com.mysql.jdbc.Driver");
        hikari.setJdbcUrl("jdbc:mysql://" + "localhost" + ":" + "3306" + "/" + "plugin");
        hikari.setUsername("Server1");
        hikari.setPassword("227VU07dickCQjav");
    }
}

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>Test</groupId>
    <artifactId>Test</artifactId>
    <version>1.0-SNAPSHOT</version>


    <dependencies>
    <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.17</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.zaxxer/HikariCP -->
        <dependency>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
            <version>3.4.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>test.Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

</project>

stacktrace: https://pastebin.com/fNQ7EFnQ

I hav posted an issue on the Hikaricp Github, it might not fit on there as it seems to be a problem only for me, but hey! maybe he can help. I do not know why this is happening, i suspect that other people can complie HikariCP just fine, the question is why dosen't it work for me...

Edit: it seems like something similar has happend before:

https://www.spigotmc.org/threads/hikaricp-with-spigot-not-importing-with-the-jar.246851/ Checking if I can get my problem to fix itself using this information.

Okay By adding the new arguments to my pom.xml i have now removed the NoClassDefFoundError and have now gotten this: https://pastebin.com/9DU9Tqra

But hey its a warning not a crash dump. meaning that it worked yay!

Missing details? Ask and I will see what i can do.


Solution

  • Problem is NoClassDefFoundError.

    What fixed it? I fixed it following a post on spigotmc:

    https://www.spigotmc.org/threads/hikaricp-with-spigot-not-importing-with-the-jar.246851/

    and added:

    <build>
        <finalName>${project.name}</finalName>
        <defaultGoal>clean package</defaultGoal>
    
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
    
                <includes>
                    <include>*</include>
                </includes>
            </resource>
        </resources>
    
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
    
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
    
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.4.3</version>
    
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
    
                <configuration>
                    <createDependencyReducedPom>false</createDependencyReducedPom>
                </configuration>
            </plugin>
        </plugins>
    </build>
    

    to my pom.xml after adding it I used "clean" then "package" using maven and tried it and it worked. however i do get a warning:

    [23:02:45] [Craft Scheduler Thread - 0/WARN]: SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
    [23:02:45] [Craft Scheduler Thread - 0/WARN]: SLF4J: Defaulting to no-operation (NOP) logger implementation
    [23:02:45] [Craft Scheduler Thread - 0/WARN]: SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
    

    However this is beside the original question. I will select this as the answer unless someone finds a better solution.