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

Maven项目编译报错:Exception in thread "main" java.lang.NoClassDefFoundError求助

解决Java.lang.NoClassDefFoundError: org/json/simple/parser/JSONParser问题

问题根源

NoClassDefFoundError表示编译阶段能找到JSONParser类,但运行时找不到对应的类文件,核心原因是运行时类路径未包含json-simple依赖库,或者你的Maven打包配置未将依赖打包进最终可执行JAR。

修复步骤

1. 修正pom.xml的错误配置

你的pom里存在两个关键错误:

  • 错误地将maven-assembly-plugin作为依赖添加到<dependencies>中,插件不属于依赖库,应该放在<build/plugins>下;
  • assembly-plugin配置在<pluginManagement>中,这只是插件版本管理,不会实际执行打包动作,需要在<build/plugins>中显式引用。

修改后的正确pom.xml片段:

<dependencies>
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>1.1.1</version>
    </dependency>
    
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
        <plugins>
            <!-- 保留你原有的其他插件配置 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.4.1</version>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <mainClass>com.myapp.app.App</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </pluginManagement>
    
    <!-- 显式引用assembly插件,让配置生效 -->
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
        </plugin>
    </plugins>
</build>

2. 修复代码中的导入缺失

你的代码使用了FileReader但未导入,会导致编译错误,需要在代码顶部添加:

import java.io.FileReader;

3. 正确打包并运行

  • 执行Maven打包命令:
mvn clean package

执行完成后,在target目录下会生成两个JAR文件:

  • 普通JAR(如your-app-1.0-SNAPSHOT.jar):仅包含你的代码,不包含依赖;
  • 带依赖的JAR(如your-app-1.0-SNAPSHOT-jar-with-dependencies.jar):包含所有依赖库,可直接运行。
  • 运行带依赖的JAR:
java -jar target/your-app-1.0-SNAPSHOT-jar-with-dependencies.jar

额外检查点

  • 执行mvn clean install确保本地仓库已正确下载json-simple依赖;
  • 确认mainClass配置的路径(com.myapp.app.App)与你的主类完全一致,大小写和包名不能错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 02:30:39