问题描述
我在使用基于 Java 的 Lambda 函数设置接收来自 SNS 的消息时遇到困难.我的函数如下所示:
I am having difficulty with a Java based Lambda function setup to receive messages from SNS. My function looks like the below:
package com.mycompany;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
import com.amazonaws.services.lambda.runtime.events.SNSEvent;
public class LambdaHandler {
public void Handler(SNSEvent event, Context context) {
//Process the event
}
}
它编译得很好,我将 jar 文件上传到 Lambda(通过网络控制台)没有任何问题.
It compiles just fine and I don't have any problems uploading the jar file to Lambda (via the web console).
但是,当我使用表示 SNSEvent 模型的 JSON 向它发布(通过 SNS 到订阅的 Lambda 函数)时,Lambda 函数会引发以下异常:
However, when I publish to it (via SNS through to the subscribed Lambda function) with JSON representing the SNSEvent model, the Lambda function throws the following exception:
在 com.mycompany.LambdaHandler 类上加载方法处理程序时出错:类 java.lang.NoClassDefFoundError java.lang.NoClassDefFoundError:com/amazonaws/services/lambda/runtime/events/SNSEvent 位于
java.lang.Class.getDeclaredMethods0(Native Method) 在java.lang.Class.privateGetDeclaredMethods(Class.java:2701) 在java.lang.Class.privateGetPublicMethods(Class.java:2902) 在java.lang.Class.getMethods(Class.java:1615) 引起:java.lang.ClassNotFoundException:com.amazonaws.services.lambda.runtime.events.SNSEvent 在java.net.URLClassLoader.findClass(URLClassLoader.java:381) 在java.lang.ClassLoader.loadClass(ClassLoader.java:424) 在java.lang.ClassLoader.loadClass(ClassLoader.java:357)
java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) at java.lang.Class.privateGetPublicMethods(Class.java:2902) at java.lang.Class.getMethods(Class.java:1615) Caused by: java.lang.ClassNotFoundException: com.amazonaws.services.lambda.runtime.events.SNSEvent at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
我使用 Maven + Netbeans,这是一个 Maven Java 应用程序项目.我从 Lambda 控制台下载了该函数并确认,jar 有一个 lib/目录,其中包含用于导入的所有 jar,包括 aws-lambda-java-events-1.1.0.jar,它本身包含/com/amazonaws/services/lambda/runtime/events/SNSEvent.class 文件.
I use Maven + Netbeans and it's a Maven Java Application project. I downloaded the function from the Lambda console and confirmed, the jar has a lib/ directory with all of the jar's for the imports, including aws-lambda-java-events-1.1.0.jar, which itself includes the /com/amazonaws/services/lambda/runtime/events/SNSEvent.class file.
当类肯定在 jar 文件中时,为什么运行时无法找到它?我还有什么需要做的吗,设置任何环境变量等?
Why is the runtime unable to find the class when it's definitely in the jar file? Is there anything else I need to do, set any environment variables, etc?
任何帮助将不胜感激!
编辑 1我尝试降级到 aws-lambda-java-events 1.0.0,但它仍然报告相同的异常.根据要求,下面是我的 POM 文件(仅更改了项目名称).我不知道如何告诉 Maven 将库放在树结构中.
EDIT 1I tried downgrading to aws-lambda-java-events 1.0.0 and it's still reporting the same exception. As requested, below is my POM file (with just project name changed). I don't know how to tell Maven to put the libraries in a tree structure.
<?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.app</groupId>
<artifactId>Handler</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-lambda</artifactId>
<version>1.10.6</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-core</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-lambda-java-events</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>
推荐答案
使用 maven-shade 插件,以便 JAR 包含 uber-jar 中的依赖项.
Use the maven-shade plugin so that the JAR contains the dependencies in an uber-jar.
因此,将其添加到您的 pom.xml
So, add this to your pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
来源:http://docs.aws.amazon.com/lambda/latest/dg/java-create-jar-pkg-maven-no-ide.html
您可能有这个问题 https://github.com/aws/aws-lambda-java-libs/issues/2 需要降级到 aws-lambda-java-events-1.0.0.jar
Potentially you may have this issue https://github.com/aws/aws-lambda-java-libs/issues/2 which requires a downgrade to aws-lambda-java-events-1.0.0.jar
这篇关于AWS Lambda NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!