AWS Lambda: class java.lang.ClassNotFoundException

2019-06-16 21:26发布

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. enter image description here

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 - enter image description here

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 - enter image description here

Once the zip is created I uploaded it to AWS Web console under the code tab - enter image description here

The Configuration tab looks something like this - enter image description here

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 -

enter image description here

2条回答
你好瞎i
2楼-- · 2019-06-16 21:54

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

查看更多
女痞
3楼-- · 2019-06-16 21:57

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.

查看更多
登录 后发表回答