AWS Lambda:ClassNotFoundException [英] AWS Lambda: ClassNotFoundException

查看:152
本文介绍了AWS Lambda:ClassNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我尝试在AWS Lambda上测试我的Lambda函数时,我当前都会收到ClassNotFoundException。此处显示例外情况:





当我将JAR文件上传到AWS Lambda控制台时,配置选项卡如下所示:[



我之前被告知过它可能是因为我的JAR文件不是带有MANIFEST.MF文件的可执行JAR文件,但我肯定有。



为什么这个错误会一直弹出以及如何修复它的其他任何原因?

解决方案

您的处理程序需要包含完整的Java包。在您的示例中,您需要具有以下处理程序:



edu.csulb.android.riseandshine.Dynamodb :: handleRequest



这是在Lambda屏幕上配置的,你当前有 Dynamodb :: handleRequest



编辑



我的hello worldLambda如下所示。请注意,这是一个maven项目,因此代码必须存在于maven期望的位置。您正在开发的目录的根是pom.xml文件(如下所示),Java文件需要存在于 src / main / java / com / hotjoe / aws / lambda /中你好/处理程序



安装完maven后,运行 mvn clean package 。可部署的jar将是 target / hello-world-lambda-1.0-SNAPSHOT.jar 。我刚刚将它部署到Lambda并且可以通过测试运行它:

  {
key3:value3 ,
key2:value2,
key1:value1
}

这是Lambda测试的默认值。这全部取自 AWS文档。在我的示例中,Lambda处理程序是 com.hotjoe.aws.lambda.hello.handler.HelloWorldLambdaHandler :: handleRequest



我使用的代码如下。



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)
公共类HelloWorldLambdaHandler实现RequestHandler< HelloWorldLambdaHandler.InputObject,String> {

public String handleRequest(InputObject inputObject,Context context){

System.out.println(got \+ inputObject +\from call );

返回{\result \:\hello lambda java \};
}

公共静态类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(){
returnInputObject {+
key1 ='+ key1 +'\ ''+
,key2 ='+ key2 +'\''+
,key3 ='+ key3 +'\''+
'}';
}
}
}

pom.xml:

 <?xml version =1.0encoding =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>


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?

解决方案

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>

这篇关于AWS Lambda:ClassNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆