AWS LAMBDA的NoClassDefFoundError [英] AWS Lambda NoClassDefFoundError

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

问题描述

我有困难了基于Java的lambda函数设置从SNS接收消息。我的功能看起来如下:

 包com.mycompany;

进口com.amazonaws.services.lambda.runtime.Context;
进口com.amazonaws.services.lambda.runtime.LambdaLogger;
进口com.amazonaws.services.lambda.runtime.events.SNSEvent;

公共类LambdaHandler {
    公共无效处理程序(SNSEvent事件,上下文语境){
        //处理事件
    }
}
 

它编译就​​好了,我没有任何问题(通过Web控制台)上传的jar文件LAMBDA。

然而,当我(经由SNS的订阅lambda函数)发布到其与JSON重新presenting的SNSEvent模型,lambda函数抛出以下异常:

  在阶级com.mycompany.LambdaHandler

加载错误方法处理:   类java.lang.NoClassDefFoundError的java.lang.NoClassDefFoundError的:   COM / amazonaws /服务/λ/运行/事件/ SNSEvent在

     

java.lang.Class.getDeclaredMethods0(本机方法)在   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)

我使用Maven + Netbeans的,它是一个Maven的Java应用程序项目。我从网上下载LAMBDA控制台的功能和确认后,罐子有一个lib /目录中的所有jar的为进口,包括AWS-λ-java的事件 - 1.1.0.jar,它本身包括/ COM / amazonaws /services/lambda/runtime/events/SNSEvent.class文件。

为什么是运行时无法找到类时,它绝对是jar文件?还有什么我需要做的,设置任何环境变量,等等?

任何帮助将是AP preciated!

修改1 我试图降级到AWS-λ-java的事件1.0.0,它仍然报告相同的异常。按照要求,下面是我的POM文件(只有项目名称更改)。我不知道如何告诉Maven来把库中的树结构。

 < XML版本=1.0编码=UTF-8&GT?;
<项目的xmlns =htt​​p://maven.apache.org/POM/4.0.0的xmlns:XSI =htt​​p://www.w3.org/2001/XMLSchema-instanceXSI:的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的>处理程序和LT; / artifactId的>
    <版> 1.0-SNAPSHOT< /版本>
    <包装>罐子< /包装>
    <依赖>
        <依赖>
            <的groupId> com.amazonaws< /的groupId>
            < artifactId的> AWS-java的SDK-拉姆达< / artifactId的>
            <版> 1.10.6< /版本>
        < /依赖性>
        <依赖>
            <的groupId> com.amazonaws< /的groupId>
            < artifactId的> AWS-λ-Java的核心和LT; / artifactId的>
            <版> 1.0.0< /版本>
        < /依赖性>
        <依赖>
            <的groupId> com.amazonaws< /的groupId>
            < artifactId的> AWS-λ-java的事件< / artifactId的>
            <版> 1.0.0< /版本>
        < /依赖性>
    < /依赖性>
    <性状>
        < project.build.sourceEncoding> UTF-8< /project.build.sourceEncoding>
        < maven.compiler.source> 1.8< /maven.compiler.source>
        < maven.compiler.target> 1.8< /maven.compiler.target>
    < /性状>
< /项目>
 

解决方案

使用行家-shade插件这样的JAR包含在尤伯杯罐子的依赖关系。

因此​​,添加到您的pom.xml

 <建立>
<插件>
  <插件>
    <的groupId> org.apache.maven.plugins< /的groupId>
    < artifactId的>的maven-遮阳插件< / artifactId的>
    <版> 2.3< /版本>
    <结构>
      < createDependencyReducedPom>假< / createDependencyReducedPom>
    < /结构>
    <执行>
      <执行>
        <阶段>包< /阶段>
        <目标>
          <目标>遮阳< /目标>
        < /目标>
      < /执行>
    < /执行>
  < /插件>
< /插件>
 

来源:的http://docs.aws.amazon.com/lambda/latest/dg/java-create-jar-pkg-maven-no-ide.html

潜在可能有这个问题 https://github.com/ AWS / AWS-λ-java的库/问题/ 2 这需要一个降级到AWS-λ-java的事件-1.0.0.jar

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
    }
}

It compiles just fine and I don't have any problems uploading the jar file to Lambda (via the web console).

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:

Error loading method handler on class com.mycompany.LambdaHandler: class java.lang.NoClassDefFoundError java.lang.NoClassDefFoundError: com/amazonaws/services/lambda/runtime/events/SNSEvent at

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)

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.

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?

Any help would be appreciated!

EDIT 1 I 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>

解决方案

Use the maven-shade plugin so that the JAR contains the dependencies in an uber-jar.

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>

Source: http://docs.aws.amazon.com/lambda/latest/dg/java-create-jar-pkg-maven-no-ide.html

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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