0

我正在尝试使用 dynamo db 运行样板代码。我正在使用 maven shade 并且在编译时一切正常,但我java.lang.NoClassDefFoundError: software/amazon/awssdk/services/dynamodb/DynamoDbClient在运行时得到了。我花了至少 5 个小时尝试不同版本的 sdk,但没有任何帮助。我可以在 Intellije 的外部库区域中看到该库。

错误:

java.lang.NoClassDefFoundError:软件/amazon/awssdk/services/dynamodb/DynamoDbClient

Pom.xml https://pastebin.com/wwaTm8yq

导致错误的代码:

DynamoDbClient ddb = DynamoDbClient.builder().build();

Map<String, AttributeValue> item = new HashMap<>();
item.put("uuid", AttributeValue.builder().s("test").build());
ddb.putItem(PutItemRequest.builder().item(item).build());
4

1 回答 1

0

看起来您的 POM 文件有问题。尝试使用AWS Github 存储库中的 V2 代码。您还可以找到此示例的 POM 文件。

例如,试试这个代码:

package com.example.dynamodb;

// snippet-start:[dynamodb.java2.list_tables.import]
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.model.DynamoDbException;
import software.amazon.awssdk.services.dynamodb.model.ListTablesResponse;
import software.amazon.awssdk.services.dynamodb.model.ListTablesRequest;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import java.util.List;
// snippet-end:[dynamodb.java2.list_tables.import]

/**
 * Lists Amazon DynamoDB tables for the current AWS account
 *
 * This code expects that you have AWS credentials set up, as described here:
 * http://docs.aws.amazon.com/java-sdk/latest/developer-guide/setup-credentials.html
 */
public class ListTables {

    public static void main(String[] args) {

        System.out.println("Your DynamoDB tables:\n");

        // Create the DynamoDbClient object
        Region region = Region.US_EAST_1;
        DynamoDbClient ddb = DynamoDbClient.builder().region(region).build();
        listAllTables(ddb);
    }

    // snippet-start:[dynamodb.java2.list_tables.main]
    public static void listAllTables(DynamoDbClient ddb){

        boolean moreTables = true;
        String lastName = null;

        while(moreTables) {
            try {
                ListTablesResponse response = null;
                if (lastName == null) {
                    ListTablesRequest request = ListTablesRequest.builder().build();
                    response = ddb.listTables(request);
                } else {
                    ListTablesRequest request = ListTablesRequest.builder()
                            .exclusiveStartTableName(lastName).build();
                    response = ddb.listTables(request);
                }

                List<String> tableNames = response.tableNames();

                if (tableNames.size() > 0) {
                    for (String curName : tableNames) {
                        System.out.format("* %s\n", curName);
                    }
                } else {
                    System.out.println("No tables found!");
                    System.exit(0);
                }

                lastName = response.lastEvaluatedTableName();
                if (lastName == null) {
                    moreTables = false;
                }
            } catch (DynamoDbException e) {
                System.err.println(e.getMessage());
                System.exit(1);
            }
        }
        // snippet-end:[dynamodb.java2.list_tables.main]
        System.out.println("\nDone!");
    }
}
于 2020-09-28T20:30:42.993 回答