AWS S3创建Bucket遇java.lang.NoSuchFieldError: JAVA_VENDOR问题
NoSuchFieldError: JAVA_VENDOR When Creating S3 Bucket with AWS SDK for Java Let me share how I resolved this frustrating error when working with the AWS SDK for Java to create an S3 bucket.
The Error I Encountered
When running my code, I hit this exact error:
Exception in thread "main" java.lang.NoSuchFieldError: JAVA_VENDOR
The stack trace pointed directly to methods in software.amazon.awssdk.core.internal.util.UserAgentUtils.
My Environment
- Java: OpenJDK 11 (version 11.0.1)
- Build Tool: Apache Maven 3.5.4
What I Tried First
I initially suspected a missing system property, so I added this line to my code to manually set the vendor value:
System.setProperty("JAVA_VENDOR","Oracle Corporation");
This didn't fix the problem. Digging into the SDK code, I realized the real issue: UserAgentUtils was trying to reference a JAVA_VENDOR field that doesn't exist in software.amazon.awssdk.utils.JavaSystemSettings.
The Actual Fix
The root cause was an outdated AWS SDK version. Here's what I updated in my Maven POM file to resolve it:
1. Update the AWS SDK BOM Version
In the dependencyManagement section, I upgraded the BOM version from 2.0.0 to 2.5.19:
<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>
2. Add the sdk-core Dependency
I also explicitly added the sdk-core dependency to ensure all required core SDK classes are properly included:
<dependencies> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>sdk-core</artifactId> </dependency> <!-- Include your other AWS SDK dependencies (like s3) here --> </dependencies>
After making these changes, the error vanished entirely, and I could successfully create the S3 bucket as intended.
内容的提问来源于stack exchange,提问作者NyansusCoder

