AWS开发工具包NoClassDefFoundError

时间:2017-01-24 22:06:25

标签: java maven amazon-web-services amazon-s3 aws-sdk

我目前正在尝试在我的项目中使用AWS SDK(特别是s3 SDK),但不断获得异常

java.lang.NoClassDefFoundError:com/amazonaws/services/s3/AmazonS3ClientBuilder

我已使用maven将SDK导入我的项目,如SDK文档here中所示。我正在运行的导致这种情况发生的代码是

AmazonS3 amazonS3 = AmazonS3ClientBuilder.standard().withRegion(Regions.EU_WEST_1).build();

我怀疑这个问题可能是由于杰克逊版本在AWS SDK所要求的版本与我自己导入的版本之间发生冲突而发生的,否则在项目的其他地方使用虽然改变了这一点似乎没有解决问题。我将在下面添加我的POM.xml文件。

提前致谢。

<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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.projectname.restservice</groupId>
<artifactId>ProjectName</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>ProjectName</name>

<build>
    <finalName>ProjectName</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <inherited>true</inherited>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey</groupId>
            <artifactId>jersey-bom</artifactId>
            <version>2.16</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-bom</artifactId>
            <version>1.11.22</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
    </dependency>

    <!--Removed due to possible conflict with AWS SDK?-->
    <!--<dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
    </dependency>-->

    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.35</version>
    </dependency>

    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk-s3</artifactId>
    </dependency>

</dependencies>

输出:mvn dependency:tree -Dverbose -Dincludes=com.amazonaws

com.trailfinder.restservice:TrailFinderRestService:war:1.0-SNAPSHOT
[INFO] \- com.amazonaws:aws-java-sdk-s3:jar:1.11.22:compile
[INFO]    +- com.amazonaws:aws-java-sdk-kms:jar:1.11.22:compile
[INFO]    |  \- (com.amazonaws:aws-java-sdk-core:jar:1.11.22:compile - omitted for duplicate)
[INFO]    \- com.amazonaws:aws-java-sdk-core:jar:1.11.22:compile

1 个答案:

答案 0 :(得分:0)

我想,我已经找到了解决此问题的解决方案/解决方法。感谢来自@DaveMaple的建议,我尝试通过命令行构建应用程序并手动部署.war,这似乎有效。

根据this question IntelliJ IDEA使用自己的构建过程而不是Mavens。这让我相信问题出在IntelliJ的构建过程中,与AWS SDK或Maven无关。要解决此问题,我将IntelliJ配置为不使用自己的构建过程,而是使用Maven,然后部署生成的.war。有关如何执行此操作的说明,请参见here.