使用Maven 打Jar包, 运行时引用的外部Jar包报 ClassNotFoundException

1.放外部JAR包到项目中

 

2.pom.xml 配置,注意: 添加classpath缺少的内容,如果不添加,生成的JAR包的MANIFEST.MF文件中会缺少对应的引用。

<build>
    <plugins>
        <!-- java编译插件 -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <encoding>UTF-8</encoding>
                <compilerArguments>
                    <extdirs>${project.basedir}/src/main/resources/lib</extdirs>
                </compilerArguments>
            </configuration>
        </plugin>
        <plugin>
      
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
出现 `ClassNotFoundException` 的原因可能是因为在打包时没有将依赖包一起打进去,导致在运行时找不到相关的类。可以尝试以下几种解决方法: 1. 在 pom.xml 文件中添加以下配置,将依赖包打进可执行的 jar 包中: ```xml <build> <plugins> <!-- 这里是maven-shade-plugin插件的配置 --> <plugin> <!-- 打包可执行的jar包 --> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.2.1</version> <configuration> <!-- 不需要打包源码 --> <shadedArtifactAttached>false</shadedArtifactAttached> <shadedClassifierName>allinone</shadedClassifierName> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>xxx.xxx.xxx.Application</mainClass> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.handlers</resource> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer"> <resource>META-INF/spring.schemas</resource> </transformer> </transformers> </configuration> <executions> <execution> <!-- 执行打包命令:mvn package --> <phase>package</phase> <goals> <goal>shade</goal> </goals> </execution> </executions> </plugin> </plugins> </build> ``` 2. 如果使用了 IntelliJ IDEA,可以在打包时勾选 "Build JAR",并在下拉框中选择 "Build JAR from modules with dependencies"。 3. 如果是使用命令行打包,则可以使用以下命令: ```bash mvn clean package -Dmaven.test.skip=true ``` 其中 `-Dmaven.test.skip=true` 表示跳过测试。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值