Home > Software engineering >  AWS Lambda java.lang.ClassNotFoundException: com.opencsv.exceptions.CsvException
AWS Lambda java.lang.ClassNotFoundException: com.opencsv.exceptions.CsvException

Time:05-15

I am getting below mentioned 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 should I do. Only when I am adding openCSV this error is occuring without it my lambda is working fine.

CodePudding user response:

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.

  • Related