简体   繁体   中英

AWS Lambda java.lang.ClassNotFoundException: com.opencsv.exceptions.CsvException

I am getting the following error while running code on AWS lambda:

    START RequestId: cd42e5ab-fbc8-4f50-8c4f-e8257525bd78 Version: $LATEST
Error loading method handler on class com.test.myapp: java.lang.NoClassDefFoundError
java.lang.NoClassDefFoundError: com/opencsv/exceptions/CsvException
    at java.base/java.lang.Class.getDeclaredMethods0(Native Method)
    at java.base/java.lang.Class.privateGetDeclaredMethods(Unknown Source)
    at java.base/java.lang.Class.privateGetPublicMethods(Unknown Source)
    at java.base/java.lang.Class.getMethods(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.opencsv.exceptions.CsvException. Current classpath: file:/var/task/

I am working on IntelliJ created a JAR and have uploaded it to AWS Lambda using console.

This is 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>org.example</groupId>
    <artifactId>testerLambda2</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-s3 -->
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-s3</artifactId>
            <version>1.12.204</version>
        </dependency>

        <dependency>
            <groupId>com.opencsv</groupId>
            <artifactId>opencsv</artifactId>
            <version>5.2</version>
        </dependency>

    </dependencies>


    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

</project>

Not sure what I should do. This error only occurs when I am adding openCSV , my lambda works fine otherwise.

Upload a FAT jar or jar-with-dependencies jar to AWS & then run the lambda.

Use maven assembly or shade plugins.
Shade Plugin:

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

Generate a FAT jar with mvn package and deploy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM