线程“主”中的异常java.lang.NoSuchFieldError:JAVA_VENDOR

时间:2019-03-30 08:37:21

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

当我尝试在AWS s3上创建存储桶时,出现以下错误: 线程“主”中的异常java.lang.NoSuchFieldError:JAVA_VENDOR     在software.amazon.awssdk.core.internal.util.UserAgentUtils.userAgent(UserAgentUtils.java:87)     在software.amazon.awssdk.core.internal.util.UserAgentUtils.initializeUserAgent(UserAgentUtils.java:73)中。

我有openjdk 11; java -version 命令

的结果
openjdk version "11.0.1" 2018-10-16
OpenJDK Runtime Environment 18.9 (build 11.0.1+13)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.1+13, mixed mode)

mvn -version 命令的结果:... Apache Maven 3.5.4 ...

这是我的pom文件:

<?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>c</groupId>
    <artifactId>e</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>

        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>s3</artifactId>
            <version>2.5.19</version>
        </dependency>

        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>regions</artifactId>
            <version>2.5.19</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-nop -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-nop</artifactId>
            <version>1.7.25</version>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>software.amazon.awssdk</groupId>
                <artifactId>bom</artifactId>
                <version>2.0.0</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

通过aws简单创建存储桶示例:

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.CreateBucketConfiguration;
import software.amazon.awssdk.services.s3.model.CreateBucketRequest;

public class Main {


    public static void main(String[] args) {
        System.setProperty("JAVA_VENDOR","Oracle Corporation");

        Region region = Region.US_WEST_2;
        S3Client s3 = S3Client.builder().region(region).build();
        String bucket = "bucket" + System.currentTimeMillis();
        CreateBucketRequest createBucketRequest = CreateBucketRequest
                .builder()
                .bucket(bucket)
                .createBucketConfiguration(CreateBucketConfiguration.builder()
                        .locationConstraint(region.id())
                        .build())
                .build();
        s3.createBucket(createBucketRequest);
    }
}

我为赢得比赛付出了一些努力: 1)试试:System.setProperty("JAVA_VENDOR","Oracle Corporation");, 因为真正的系统属性具有中庸的“ java.vendor” 2)我在software.amazon.awssdk.core.internal.util.UserAgentUtils.userAgent中发现了问题所在:它从software.amazon.awssdk.utils.JavaSystemSettings调用JAVA_VENDOR

.replace("{java.vendor}", sanitizeInput(JavaSystemSetting.JAVA_VENDOR.getStringValue().orElse(null)))

但是JavaSystemSettings类中没有具有该名称的字段。 我该如何处理?

TR; DR 更新! 它适用于下一个Maven Pom文件:

<?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>c</groupId>
<artifactId>e</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-nop</artifactId>
        <version>1.7.25</version>
    </dependency>
    <dependency>
        <groupId>software.amazon.awssdk</groupId>
        <artifactId>sdk-core</artifactId>
        <version>2.5.19</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/software.amazon.awssdk/s3 -->
    <dependency>
        <groupId>software.amazon.awssdk</groupId>
        <artifactId>s3</artifactId>
        <version>2.5.19</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/software.amazon.awssdk/regions -->
    <dependency>
        <groupId>software.amazon.awssdk</groupId>
        <artifactId>regions</artifactId>
        <version>2.5.19</version>
    </dependency>
</dependencies>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>bom</artifactId>
            <version>2.5.19</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

1 个答案:

答案 0 :(得分:2)

NoSuchFieldError是由加载类时的版本不匹配引起的。

(设置属性无法解决此问题。异常是指类的字段,而不是Properties对象中的属性。)

  

但是JavaSystemSettings类中没有具有该名称的字段。

是的!包含UserAgentUtils类和JavaSystemSettings类的JAR之间版本不匹配。