java.lang.ClassNotFoundException 在 AWS CDK 代码中引用 Lambda 代码时
java.lang.ClassNotFoundException while referencing Lambda code in AWS CDK code
我是 AWS CDK 的新手。
问题: 我正在尝试将我的 lambda 代码引用到 CDK 代码中,但是
当我尝试在 AWS Lambda 控制台.
上测试它时,它抛出 java.lang.ClassNotFoundException
我添加了一些代码以供参考。 classes 存在于同一个包中。
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 代码引用到 CDK 代码中,但是 当我尝试在 AWS Lambda 控制台.
上测试它时,它抛出java.lang.ClassNotFoundException
我添加了一些代码以供参考。 classes 存在于同一个包中。
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)
创建部署包后有效。