Reputation: 173
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, including this link here: AWS Lambda: class java.lang.ClassNotFoundException, to no avail.
I am working in Android Studio and created a JAR file (using this link: How to make a .jar out from an Android Studio project) to use to upload the class to the AWS Lambda console.
Below is the structure of my project:
When I upload my JAR file to the AWS Lambda console, the Configuration tab looks like this:[
I was previously told that it could have been because my JAR file was not an executable JAR file with a MANIFEST.MF file, but I definitely have that.
Any other reason as to why this error consistently pops up and how to fix it?
Upvotes: 17
Views: 39559
Reputation: 748
I know it's late to share the solution,
but the same problem I was facing in Apr, 2021 and none of these answers worked in my case. so I'm sharing how I fixed it, it may help someone.
I got it resolved by adding following maven plugin with spring-boot-thin-layout to create a thin jar.
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot.experimental</groupId>
<artifactId>spring-boot-thin-layout</artifactId>
<version>1.0.26.RELEASE</version>
</dependency>
</dependencies>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>aws</shadedClassifierName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer implementation="org.springframework.boot.maven.PropertiesMergingResourceTransformer">
<resource>META-INF/spring.factories</resource>
</transformer>
<transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
</transformers>
</configuration>
and then you can run
./mvnw clean package
or if you're using STS/Eclipse, then run Maven build.. with goals clean package
This will generate a jar file in KB in your target folder and you can use this JAR to deploy on AWS lambda code.
PS:
Upvotes: 0
Reputation: 370
It's not an answer to the original question. But I was facing the same issue of class not found and solved it by placing pom.xml at the correct location in the directory structure. In Android Studio, "src/main/java/.." lives in the Application folder inside the root directory. I was wrongly placing the xml file in the root directly.
When I created a separate directory structure outside of Android Project and placed the xml file as described here, the problem was resolved.
Upvotes: 0
Reputation: 1948
I fixed my issue by following below link. Basically need to run mvn "package shade:shade" command to include all depending jars. https://docs.aws.amazon.com/lambda/latest/dg/java-create-jar-pkg-maven-and-eclipse.html (Later experiments showed that just do mvn package would be enough as long as the shade plugin defined in pom.xml file.)
The next challenge I faced is the jar too big. I followed the below link to include dynomaDB, S3, ec2 components instead of the entire sdk. https://aws.amazon.com/blogs/developer/managing-dependencies-with-aws-sdk-for-java-bill-of-materials-module-bom/.
Then I need to use EnvironmentVariableCredentialsProvider to deploy to lambda function.
Upvotes: 1
Reputation: 17435
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<HelloWorldLambdaHandler.InputObject, String> {
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:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hotjoe.aws.lambda.hello</groupId>
<artifactId>hello-world-lambda</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Upvotes: 12
Reputation: 772
The stack trace indicates that the Java runtime cannot find a class named "Dynamodb". There is no such class in the AWS SDK for Java .. the correct class name is "DynamoDB". Notice the difference in case between your class from the exception stack trace and the correct name.
Upvotes: 0