“java.lang.ClassNotFoundException:software.amazon.awssdk.regions.Region”error for JAR built via IntelliJ

ua4mk5z4  于 2023-04-10  发布在  Java
关注(0)|答案(1)|浏览(135)

我正在使用IntelliJ开发一个AWS应用程序(只需从SecretsManager中检索)。我能够在IntelliJ中正常运行该应用程序,但当我通过IntelliJ构建JAR工件并尝试执行JAR时,我会得到java.lang.ClassNotFoundException: software.amazon.awssdk.regions.Region
我通过IntelliJ构建了JAR,但我使用pom.xml来指定依赖项。当我尝试执行JAR时,我得到java.lang.ClassNotFoundException: software.amazon.awssdk.regions.Region错误。我已经将此依赖项作为pom.xml的一部分包含在内,并且我还验证了JAR正在提取“software.amazon.awssdk.regions”库。
我不知道为什么JAR会抱怨“ClassNotFoundException”,而它要查找的类实际上就在JAR中?
任何指导都是非常感谢的。
下面是我的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.chronoslabs</groupId>
    <artifactId>chronos-trader</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>software.amazon.awssdk</groupId>
                <artifactId>bom</artifactId>
                <version>${aws.java.sdk.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.jmockit/jmockit -->
        <dependency>
            <groupId>org.jmockit</groupId>
            <artifactId>jmockit</artifactId>
            <version>1.49</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.github.binance</groupId>
            <artifactId>binance-connector-java</artifactId>
            <version>1.12.0</version>
            <scope>compile</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.9.1</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.7.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.jmockit</groupId>
            <artifactId>jmockit</artifactId>
            <version>1.49</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>31.1-jre</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.knowm.xchange/xchange-core -->
        <dependency>
            <groupId>org.knowm.xchange</groupId>
            <artifactId>xchange-core</artifactId>
            <version>5.0.14</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.knowm.xchange/xchange-stream-core -->
        <dependency>
            <groupId>org.knowm.xchange</groupId>
            <artifactId>xchange-stream-core</artifactId>
            <version>5.0.14</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.knowm.xchange/xchange-stream-service-netty -->
        <dependency>
            <groupId>org.knowm.xchange</groupId>
            <artifactId>xchange-stream-service-netty</artifactId>
            <version>5.0.14</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.knowm.xchange/xchange-binance -->
        <dependency>
            <groupId>org.knowm.xchange</groupId>
            <artifactId>xchange-binance</artifactId>
            <version>5.0.14</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.19.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.19.0</version>
        </dependency>
        <!-- https://logging.apache.org/log4j/2.x/manual/async.html -->
        <dependency>
            <groupId>com.lmax</groupId>
            <artifactId>disruptor</artifactId>
            <version>3.4.4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/software.amazon.awssdk/secretsmanager -->
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>secretsmanager</artifactId>
            <version>2.20.38</version>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>regions</artifactId>
            <version>2.20.38</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>

        <aws.java.sdk.version>2.20.38</aws.java.sdk.version>
    </properties>

</project>
t2a7ltrp

t2a7ltrp1#

最终帮助我的是maven-assembly-plugin。这将创建一个JAR,其中捆绑了所有依赖项。

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>

相关问题