java – AWS Lambda:ClassNotFoundException
Whenever I try to test my lambda function on AWS lambda, I currently get a classnotfoundexception. The exception is shown here:
I have searched on the Internet, including this link: AWS lambda: class java.lang.classnotfoundexception, which is useless
I worked in Android studio and created a jar file (using this link: how to make a. Jar out from an Android studio project) to upload classes to the AWS lambda console
Here is my project structure:
When I upload the jar file to the AWS lambda console, the configuration tab is as follows:[
I was told that it might be because my jar file is not an executable jar file with manifest.mf file, but I must have
Any other reason why this error keeps popping up and how to solve it?
resolvent:
Your handler needs to contain a complete java package. In your example, you need to have the following handler:
Edu. CSULB. Android. Riseandshine. Dynamodb:: handlerequest
This is configured on the lambda screen where you currently have dynamodb:: handlerequest
edit
My "Hello world" lambda looks like the following. Please note that this is a maven project, so the code must exist in the location expected by Maven. The "root" of the directory you are developing is the pom.xml file (as shown below), and the java file needs to exist in Src / main / Java / COM / hotjoe / AWS / lambda / hello / handler
After installing Maven and maven, run MVN clean package. The deployable jar will be target / hello-world-lambda-1.0-snapshot.jar. I just deployed it to lambda and can run it using tests:
{
"key3": "value3",
"key2": "value2",
"key1": "value1"
}
This is the default value of lambda test. All this is taken from AWS docs when creating deployment. In my example, the lambda handler is com.hotjoe.aws.lambda.hello.handler.helloworldlambdahandler:: handlerequest
The code I used is as follows
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 + '\'' +
'}';
}
}
}
In 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>