gpt4 book ai didi

java - 线程 "main"java.lang.NoSuchFieldError : JAVA_VENDOR 中的异常

转载 作者:行者123 更新时间:2023-11-29 08:23:33 25 4
gpt4 key购买 nike

当我尝试在 aws s3 上创建存储桶时,出现此错误:线程“main”中的异常 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;博士 更新!它适用于我的下一个 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>

最佳答案

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

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

But there is no field with such name in the JavaSystemSettings class.

没错!包含 UserAgentUtils 类和 JavaSystemSettings 类的 JAR 版本不匹配。

关于java - 线程 "main"java.lang.NoSuchFieldError : JAVA_VENDOR 中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55429636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com