You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

添加mariadb-java-client依赖后出现org.slf4j.Logger缺失错误求助

解决java.lang.NoClassDefFoundError: org.slf4j.Logger问题

1. 统一SLF4J依赖版本

SLF4J的API和绑定包(如slf4j-simple/log4j12)必须使用完全一致的版本,版本不匹配是引发这类错误的常见原因。在pom.xml中明确指定统一版本:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.36</version> <!-- 替换为你需要的稳定版本 -->
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.7.36</version>
    <scope>runtime</scope> <!-- 仅运行时生效,避免编译期依赖冲突 -->
</dependency>

2. 排除MariaDB驱动的传递SLF4J依赖

mariadb-java-client本身会依赖slf4j-api,可能引入与你手动添加版本不一致的SLF4J包。在pom.xml中给MariaDB依赖添加排除规则,强制使用你指定的SLF4J版本:

<dependency>
    <groupId>org.mariadb.jdbc</groupId>
    <artifactId>mariadb-java-client</artifactId>
    <version>3.3.3</version> <!-- 替换为你使用的版本 -->
    <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

3. 适配JavaFX模块化(JPMS)规则

如果你的项目使用了JavaFX模块化(存在module-info.java),需要在模块声明中明确引入SLF4J和MariaDB驱动的模块:

module your.module.name {
    requires javafx.controls;
    requires javafx.fxml;
    // 引入SLF4J相关模块
    requires org.slf4j;
    requires org.slf4j.simple;
    // 引入MariaDB驱动模块
    requires org.mariadb.jdbc;
    // 若Spring需要反射访问你的类,需打开对应包
    opens com.your.package to org.springframework.context;
}

4. 检查依赖Scope设置

确保SLF4J API的scope为compile(默认即可),绑定包的scope为runtime,不要设置为provided或test,否则运行时会缺失类文件。

5. 清理Maven缓存并重建项目

执行以下命令清理本地Maven缓存,避免旧依赖残留引发的冲突:

mvn clean install -U

内容的提问来源于stack exchange,提问作者Martin819

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 10:37:31