如何通过 Java 使用 AWS lambda 层(Lambda 中的层是成功的)...错误是 NoClassDefFoundError

You*_*Liu 5 java noclassdeffounderror amazon-web-services aws-lambda

我构建了两个项目并成功地向 AWS Lamba 添加了层。

而我的函数使用了这两层。

这是我的层结构

截屏

当我执行该函数时,发生了一个错误:

java.lang.NoClassDefFoundError

我知道层的位置是 inside/opt,但是如何在函数中使用层的库?

小智 5

您应该根据语言将文件定位到一个文件夹中:

  • Node.js –> nodejs/node_modules 或 nodejs/node8/node_modules (NODE_PATH)

  • Python – python -> python/lib/python3.7/site-packages(站点
    目录)

  • Java –> java/lib(类路径)

  • Ruby –> ruby​​/gems/2.5.0 (GEM_PATH), ruby​​/lib (RUBY_LIB)

  • 或者默认 All –> bin (PATH), lib (LD_LIBRARY_PATH)

有关更多详细信息,请参阅:https : //docs.aws.amazon.com/en_us/lambda/latest/dg/configuration-layers.html#configuration-layers-path


Tah*_*raf 5

任何试图弄清楚如何将您的依赖项(.jar)复制到java/lib目录中的人,这是一个项目的 maven 片段 -

    <build>
    <plugins>
            <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.compiler.plugin.version}</version>
            <configuration>
                <source>${maven.compiler.source}</source>
                <target>${maven.compiler.target}</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.8</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>prepare-package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/classes/java/lib</outputDirectory>
                        <includeScope>runtime</includeScope>
                    </configuration>
                </execution>
            </executions>
        </plugin></plugins></build>
Run Code Online (Sandbox Code Playgroud)

希望这对某人有用