Search code examples
javaspringamazon-web-servicesaws-lambdaspring-cloud-function

During lambda aws test, a class not found exception is thrown


Serverless Functions with Spring Cloud Function is the Baeldung article I was using. I changed their recommended aws-adapter dependency because importing in maven failed, so I found the version 3.0.1.RELEASE AND used that instead. All errors gone, I uploaded project as a .jar file. I tried "maven package" and "maven clean shade" commands to create a .jar file and directly uploading to the lambda function. I followed their instructions in specifying the handler in the lambda function. Does anyone know of any other reasons I might be running into this issue.

 java.lang.ClassNotFoundException: package.MyStringHandlers
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
{
  "errorMessage": "Class not found: package.MyStringHandlers",
  "errorType": "java.lang.ClassNotFoundException"
}
<dependencies>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-events</artifactId>
            <version>2.0.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-core</artifactId>
            <version>1.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot.experimental/spring-boot-thin-layout -->
        <dependency>
            <groupId>org.springframework.boot.experimental</groupId>
            <artifactId>spring-boot-thin-layout</artifactId>
            <version>1.0.23.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-function-web</artifactId>
            <version>1.0.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-function-adapter-aws</artifactId>
            <version>3.0.1.RELEASE</version>
        </dependency>


    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-deploy-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <configuration>
                    <createDependencyReducedPom>false</createDependencyReducedPom>
                    <shadedArtifactAttached>true</shadedArtifactAttached>
                    <shadedClassifierName>aws</shadedClassifierName>
                </configuration>
            </plugin>
        </plugins>
    </build>
        </project>

Solution

  • You seem to be using some mixed and incompatible versions of function together 1.0.1.RELEASE and 3.0.1.RELEASE although your problem is probably unrelated, yet it's impossible to say without seeing your code.

    The good news is:

    For 3.0.1 release we jus added a Getting Started guide which takes you step by step. We've also updated the example which is also referenced in the Getting Started. With it you should be up and running in 5-10 min.