AWS Lambda: class java.lang.ClassNotFoundException

故事扮演 提交于 2019-12-10 02:36:48

问题


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 -


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/42693229/aws-lambda-class-java-lang-classnotfoundexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!