Reputation: 161
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!
Upvotes: 4
Views: 8974
Reputation: 41
Had similar problem, adding maven-shade-plugin and rebuilding with GOAL: package shade:shade solved the issue
Upvotes: 4
Reputation: 1246
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.
Upvotes: 3
Reputation: 1258
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
Upvotes: 3