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

运行Maven构建Jar包时遇java.lang.ClassNotFoundException问题求助

解决Maven打包Jar后运行缺失IBM MQ类的问题

问题原因

NetBeans运行项目时会自动加载Maven依赖库中的所有第三方Jar包,但默认Maven打包生成的Jar仅包含你项目自身的代码,不包含IBM MQ这类依赖库的内容,所以命令行执行java -jar时会找不到对应的类。

解决办法

方法1:使用Maven Assembly插件生成包含所有依赖的可执行Jar(胖Jar)

在项目的pom.xml中添加以下插件配置:

<build>
    <plugins>
        <!-- 配置Assembly插件 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.6.0</version>
            <configuration>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
                <archive>
                    <manifest>
                        <!-- 指定主类路径 -->
                        <mainClass>com.stee.simpletestmq.SimpleTestApp</mainClass>
                    </manifest>
                </archive>
            </configuration>
            <executions>
                <execution>
                    <id>make-assembly</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

配置完成后:

  • 在NetBeans中,右键项目 → Run Maven → package,完成后在target目录下会生成类似SimpleTestMQ-1.0-SNAPSHOT-jar-with-dependencies.jar的文件
  • 命令行运行该文件:java -jar SimpleTestMQ-1.0-SNAPSHOT-jar-with-dependencies.jar

方法2:使用Maven Shade插件生成胖Jar

若Assembly插件不符合需求,可改用Shade插件,在pom.xml中添加:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.4.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.stee.simpletestmq.SimpleTestApp</mainClass>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

执行mvn clean package后,target目录下的主Jar文件已包含所有依赖,直接用java -jar运行即可。

方法3:手动指定依赖类路径运行

如果不想打包胖Jar,可将IBM MQ的依赖Jar包(可从Maven仓库中找到)复制到项目目录下的lib文件夹,然后通过以下命令运行:

  • Windows系统:
java -cp "SimpleTestMQ-1.0-SNAPSHOT.jar;lib/*" com.stee.simpletestmq.SimpleTestApp
  • Linux/macOS系统:
java -cp "SimpleTestMQ-1.0-SNAPSHOT.jar:lib/*" com.stee.simpletestmq.SimpleTestApp

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 17:40:36