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

Maven项目使用Gson时遭遇java.lang.NoClassDefFoundError的解决方法

解决Maven项目中GSON的NoClassDefFoundError问题

常见原因及解决步骤

1. 强制刷新Maven依赖

  • 打开IntelliJ的Terminal,执行命令:
    mvn clean install -U
    
    -U参数会强制更新依赖,确保GSON包被完整下载到本地Maven仓库。
  • 也可以通过IDE界面操作:点击右侧Maven面板的「Reload All Maven Projects」刷新按钮,等待依赖同步完成。

2. 检查运行配置的依赖范围

  • 进入「Run」→「Edit Configurations」,找到你的运行配置
  • 切换到「Dependencies」标签页,确认「Use classpath of module」选择的是当前项目模块,且依赖列表中包含GSON
  • 若有需要,勾选「Include dependencies with Provided scope」(GSON默认是compile scope,一般无需额外操作)

3. 清理本地Maven仓库缓存

  • 找到本地Maven仓库中GSON的存储目录(默认路径:~/.m2/repository/com/google/code/gson/gson/2.10.1)
  • 删除该目录下所有文件,重新执行mvn clean install让Maven重新下载GSON包

4. 清除IntelliJ缓存并重启

  • 点击「File」→「Invalidate Caches...」,选择「Invalidate and Restart」
  • 重启IDE后重新加载项目,解决IDE缓存导致的依赖识别问题

验证方案

添加测试代码确认GSON正常工作:

import com.google.gson.Gson;

public class ImportTest {
    public static void main(String[] args) {
        Gson gson = new Gson();
        String testJson = gson.toJson("测试GSON");
        System.out.println(testJson);
    }
}

运行该类,若控制台输出"测试GSON",说明问题已解决。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 11:03:24