通过 Maven 依赖导入 hikaricp 后出现 NoClassDefFoundError

NoClassDefFoundError after importing hikaricp through Maven dependency

在 spigot 上启动我的 Java 插件时出现 java.lang.NoClassDefFoundError: com/zaxxer/hikari/HikariDataSource 错误。据我所知,我已正确导入它。

我知道这个问题已被多次发布,但通过浏览 3-4 我没有看到任何明确的问题答案。 代码在 hikari = new HikariDataSource() 处崩溃,这也是使用的第一个 hikari 语句。

我的 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>

hikari 似乎没有编译成 jar,但我可以在编码时使用它的资产。

代码:

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();
    }
}

粘贴箱中的完整崩溃报告,以便于查看: https://pastebin.com/JEMz0f6T

我尝试使用 hikaricp 创建独立程序: 代码:

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>

堆栈跟踪:https://pastebin.com/fNQ7EFnQ

我已经在 Hikaricp Github 上发布了一个问题,它可能不适合放在那里,因为它似乎只对我有问题,但是嘿!也许他能帮忙。 我不知道为什么会这样,我怀疑其他人可以很好地遵守 HikariCP,问题是为什么它对我不起作用...

编辑:好像以前发生过类似的事情:

https://www.spigotmc.org/threads/hikaricp-with-spigot-not-importing-with-the-jar.246851/ 检查我是否可以使用此信息自行修复我的问题。

好的,通过将新参数添加到我的 pom.xml 我现在已经删除了 NoClassDefFoundError 现在得到了这个:https://pastebin.com/9DU9Tqra

但是嘿,这是一个警告,而不是故障转储。这意味着它成功了!

缺少详细信息?询问,我会看看我能做些什么。

@Hampus,我已经尝试检查在 Hicari Pool 的 3.4.0 版本中,class com.zaxxer.hikari.HikariDataSource 在包 com/zaxxer/hikari 中可用。你可以看到下面的截图。我怀疑的是您添加的存储库。请从 pom.xml 中删除以下内容并尝试。

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

我知道您可能正在使用您的 nexus,这个 jar 文件在 maven central 中可用。为此,我在下面提供了 mvn 存储库 link。

https://mvnrepository.com/artifact/com.zaxxer/HikariCP/3.4.0

问题是 NoClassDefFoundError。

修复了什么?我在 spigotmc 上按照 post 修复了它:

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

并添加:

<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>

添加到我的 pom.xml 后,我使用 "clean" 然后 "package" 使用 maven 并尝试了它并且它有效。但是我确实收到警告:

[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.

然而,这是在最初的问题旁边。 除非有人找到更好的解决方案,否则我将select作为答案。