AWS Lambda: ClassNotFoundException

前端 未结 4 1645
南方客
南方客 2021-01-02 04:00

I currently get a ClassNotFoundException whenever I try to test my Lambda function on AWS Lambda. The exception is shown here:

I\'ve searched online, inclu

4条回答
  •  半阙折子戏
    2021-01-02 04:23

    Your handler needs to include the full Java package. In your example, you need to have the handler be:

    edu.csulb.android.riseandshine.Dynamodb::handleRequest

    This is configured on the Lambda screen where you currently have Dynamodb::handleRequest

    EDIT

    My "hello world" Lambda in looks like the following. Note that this is a maven project so the code has to live where maven expects it. At the "root" of the directory where you're developing is the pom.xml file (below) and the Java file needs to live in src/main/java/com/hotjoe/aws/lambda/hello/handler.

    Once you have that and maven installed, run mvn clean package. The deployable jar will be target/hello-world-lambda-1.0-SNAPSHOT.jar. I deployed this to Lambda just now and can run it with the test:

    {
      "key3": "value3",
      "key2": "value2",
      "key1": "value1"
    }
    

    which is the default for the Lambda tests. This is all taken from the AWS docs on creating a deployment. In my example, the Lambda handler is com.hotjoe.aws.lambda.hello.handler.HelloWorldLambdaHandler::handleRequest.

    The code I've used is below.

    HelloWorldLambdaHandler.java

    package com.hotjoe.aws.lambda.hello.handler;
    
    import com.amazonaws.services.lambda.runtime.Context;
    import com.amazonaws.services.lambda.runtime.RequestHandler;
    
    
    @SuppressWarnings("unused")
    public class HelloWorldLambdaHandler implements RequestHandler {
    
        public String handleRequest(InputObject inputObject, Context context) {
    
            System.out.println( "got \"" + inputObject + "\" from call" );
    
            return "{\"result\": \"hello lambda java\"}";
        }
    
        public static class InputObject {
            private String key1;
            private String key2;
            private String key3;
    
            public String getKey1() {
                return key1;
            }
    
            public String getKey2() {
                return key2;
            }
    
            public String getKey3() {
                return key3;
            }
    
            public void setKey1(String key1) {
                this.key1 = key1;
            }
    
            public void setKey2(String key2) {
                this.key2 = key2;
            }
    
            public void setKey3(String key3) {
                this.key3 = key3;
            }
    
            @Override
            public String toString() {
                return "InputObject{" +
                        "key1='" + key1 + '\'' +
                        ", key2='" + key2 + '\'' +
                        ", key3='" + key3 + '\'' +
                        '}';
            }
        }
    }
    

    pom.xml:

    
    
        4.0.0
    
        com.hotjoe.aws.lambda.hello
        hello-world-lambda
        1.0-SNAPSHOT
    
        
            1.8
            1.8
            UTF-8
        
    
        
            
                com.amazonaws
                aws-lambda-java-core
                1.1.0
            
        
    
        
            
                
                    org.apache.maven.plugins
                    maven-shade-plugin
                    3.1.0
                    
                        false
                    
                    
                        
                            package
                            
                                shade
                            
                        
                    
                
            
        
    
    

提交回复
热议问题