Reputation: 3290
I am getting this message and I have no idea how to resolve it. Searched online and tried to implement their suggestion, but no luck yet.
I basically followed the instructions specified in this link - http://docs.aws.amazon.com/toolkit-for-eclipse/v1/user-guide/lambda-tutorial.html
But instead of uploading the project using the AWS Management Console embedded in Eclipse, I tried to create a zip of my project and upload it to the AWS web console.
Below is structure of my project -
That is it!! There is nothing fancy that I am trying to do here. It is just a HelloWorld example in Lambda.
Now, this is how I am creating the zip file, which is pretty straight forward in Eclipse -
Once the zip is created I uploaded it to AWS Web console under the code tab -
The Configuration tab looks something like this -
Now when I am clicking the Test button it is unable to find the example.Hello class.
How come it is becoming so difficult for the Lambda Function to find this class? Can anyone suggest what possibly is going wrong in this execution??
Also attached the log statement, in case it helps -
Upvotes: 17
Views: 20094
Reputation: 117
Sample java Code: filename : Main.java
package org.example;
public class Main implements RequestHandler<Map<String,String>, String> {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
@Override
public String handleRequest(Map<String, String> event, Context context) {
...}
...}
Set configuration as follows:
Handler
org.example.Main::handleRequest
Upvotes: 0
Reputation: 875
Non-Maven answer
The reason why it's not working is because Lambda is expecting compiled *.class
files in the zip. Unfortunately it won't compile the Java files for you.
I was getting the same error message as you when trying to upload a standalone Java file (not using Maven or Gradle or anything else). Although not very well-documented, I noticed this subtle detail on AWS documentation hinting at the class files (the grep part):
Example test-zip.sh
mkdir -p expanded unzip path/to/my/function.zip -d expanded find ./expanded/lib -name '*.jar' | xargs -n1 zipinfo -1 | grep '.*.class' | >sort | uniq -c | sort
When I uploaded a zip file with just the class file, Lambda ended up finding the file correctly. Although, I had to remove the AWS Context
parameter just for it to compile, so Lambda produced a different error message saying the handler method signature didn't match (it found the file though).
Upvotes: 0
Reputation: 748
Instead of maven-shade-plugin, you can use spring-boot-maven-plugin plugin with spring-boot-thin-layout dependency and other configurations as shown in below plugin.
<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>
</plugin>
PS:
Upvotes: 0
Reputation: 376
The following worked for me.
Upvotes: 14
Reputation: 36
Note that the generic type of your RequestHandler interface is different from the default one :
default: public class Hello implements RequestHandler<Object, String>
yours: public class Hello implements RequestHandler<String, String>
The reason why it is giving ClassNotFoundException
Upvotes: 0
Reputation: 1650
I had the same problem, what worked for me was if you're running this from eclipse with maven, ensure you have the following plugin in your pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>com.amazonaws:aws-lambda-java-events</exclude>
<exclude>com.amazonaws:aws-lambda-java-core</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
Then run the project with: mvn package shade:shade
to generate the jar artifacts in your target directory. After that, eclipse should upload the correct jar to lambda.
Upvotes: 3