gpt4 book ai didi

在 AWS CDK 代码中引用 Lambda 代码时出现 java.lang.ClassNotFoundException

转载 作者:行者123 更新时间:2023-12-01 13:45:27 26 4
gpt4 key购买 nike

我是新来的 AWS CDK .

问题:我正在尝试将我的 lambda 代码引用到 CDK 代码但是
它抛出 java.lang.ClassNotFoundException当我尝试在 上测试它时AWS Lambda 控制台 .

我添加了一些代码以供引用。这些类存在于同一个包中。
LambdaFunctionHandler类(class) :

Lambda 代码:

package com.myorg;

import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;

public class LambdaFunctionHandler implements RequestHandler<String, String> {

public String handleRequest(String event, Context context) {
context.getLogger().log("Received event: " + event);
return "Hello from lambda "+event;

}

}

CDK代码:
package com.myorg;

import software.amazon.awscdk.core.Construct;
import software.amazon.awscdk.core.Stack;
import software.amazon.awscdk.core.StackProps;
import software.amazon.awscdk.services.lambda.Code;
import software.amazon.awscdk.services.lambda.Function;
import software.amazon.awscdk.services.lambda.Runtime;

public class CdkProjectStack extends Stack {
public CdkProjectStack(final Construct scope, final String id) {
this(scope, id, null);
}

public CdkProjectStack(final Construct scope, final String id, final StackProps props) {
super(scope, id, props);

// The code that defines your stack goes here
final Function hello = Function.Builder.create(this, "HelloHandler")
.runtime(Runtime.JAVA_8)
.code(Code.fromAsset("src/main/java/com/myorg"))
.handler("com.myorg.LambdaFunctionHandler::handleRequest")
.build();
}
}

错误日志:
START RequestId: 0fdf618c-1bfb-4905-9283-f6b4e34a6585 Version: $LATEST
Class not found: com.myorg.LambdaFunctionHandler: java.lang.ClassNotFoundException
java.lang.ClassNotFoundException: com.myorg.LambdaFunctionHandler
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)

最佳答案

它在创建部署包后工作。

关于在 AWS CDK 代码中引用 Lambda 代码时出现 java.lang.ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62277853/

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