4

I'm triying to create my first lambda function in Java.

I want to start with a little example, reading a S3 Input Event.

It's my code:

package com.amazonaws.lambda.alfreddo;

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

public class LambdaFunctionHandler implements RequestHandler<S3Event, String> {

    @Override
    public String handleRequest(S3Event input, Context context) {
        context.getLogger().log("Input: " + input);

        // TODO: implement your handler
        return "Hello from Lambda!";
    }

}

But, when in i try to run it on AWS Console i get the next error:

{
  "errorMessage": "Error loading method handleRequest on class com.amazonaws.lambda.alfreddo.LambdaFunctionHandler",
  "errorType": "java.lang.NoClassDefFoundError"
}

Error loading method handleRequest on class com.amazonaws.lambda.alfreddo.LambdaFunctionHandler: java.lang.NoClassDefFoundError
java.lang.NoClassDefFoundError: com/amazonaws/services/lambda/runtime/events/S3Event
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.privateGetPublicMethods(Class.java:2902)
    at java.lang.Class.getMethods(Class.java:1615)
Caused by: java.lang.ClassNotFoundException: com.amazonaws.services.lambda.runtime.events.S3Event
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 4 more

I'm using the AWS Toolkit for Eclipse.

any help?

Thanks!

2
  • Are you using maven for dependency management? Commented Mar 20, 2018 at 11:34
  • Yes! When i realized that, i builded the the project with Maven, and it worked Commented Mar 20, 2018 at 15:34

3 Answers 3

4

Had similar problem, adding maven-shade-plugin and rebuilding with GOAL: package shade:shade solved the issue

Sign up to request clarification or add additional context in comments.

Comments

3

This: com/amazonaws/services/lambda/runtime/events/S3Event Isn't in your ClassPath.

If you are building a jar you have to make sure to add your dependencies, or, if you are running from the CLI make sure to explicitly add the dependency location via -cp /dir/to/location

Comments

3

It means that the dependencies required for the jar to work standalone are not included in the jar file. Thus the AWS SDK dependencies must be packaged in the jar.

Check the below link for aws documentation. https://docs.aws.amazon.com/lambda/latest/dg/java-create-jar-pkg-maven-and-eclipse.html

The important part is to use the maven-shade-plugin. Make sure it is being executed when you package the jar.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.