Skip to content

Cannot run lambda function after upgrading aws-lambda-java-events to version 1.1.0 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
chunyang opened this issue Aug 28, 2015 · 9 comments

Comments

@chunyang
Copy link

When building against aws-lambda-java-events 1.0.0, my lambda function runs correctly. However, after changing the dependency to 1.1.0, the lambda function fails with a NoClassDefFoundError

Error loading method recordHandler on class Handler: class java.lang.NoClassDefFoundError
java.lang.NoClassDefFoundError: com/amazonaws/services/lambda/runtime/events/S3Event
    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.S3Event
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 4 more

I've created a minimal working example that reproduces this problem:
https://github.com/chunyang/lambda-test

The handler Handler::recordHandler runs correctly in lambda-test-1.0.0.jar and does not run correctly in lambda-test-1.1.0.jar

@bmoffatt
Copy link
Contributor

bmoffatt commented Oct 8, 2015

Thanks for posting the example, I was able to reproduce the issue quickly.

Interestingly, when I build the same project with the same dependencies using maven, I can't reproduce the error.

@pako-pl
Copy link

pako-pl commented Nov 18, 2015

I had a similar problem with 1.1.0 and had to downgrade to 1.0.0:

Exception in thread "main" java.lang.Error: java.lang.TypeNotPresentException: Type com.amazonaws.services.lambda.runtime.events.SNSEvent not present
    at lambdainternal.AWSLambda.<clinit>(AWSLambda.java:61)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at lambdainternal.LambdaRTEntry.main(LambdaRTEntry.java:93)
Caused by: java.lang.TypeNotPresentException: Type com.amazonaws.services.lambda.runtime.events.SNSEvent not present
    at sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:117)
    at sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:125)
    at sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)
    at sun.reflect.generics.visitor.Reifier.reifyTypeArguments(Reifier.java:68)
    at sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:138)
    at sun.reflect.generics.tree.ClassTypeSignature.accept(ClassTypeSignature.java:49)
    at sun.reflect.generics.repository.ClassRepository.getSuperInterfaces(ClassRepository.java:105)
    at java.lang.Class.getGenericInterfaces(Class.java:913)
    at lambdainternal.EventHandlerLoader.findInterfaceParameters(EventHandlerLoader.java:632)
    at lambdainternal.EventHandlerLoader.wrapRequestHandlerClass(EventHandlerLoader.java:670)
    at lambdainternal.EventHandlerLoader.loadStreamingRequestHandler(EventHandlerLoader.java:725)
    at lambdainternal.EventHandlerLoader.loadEventHandler(EventHandlerLoader.java:733)
    at lambdainternal.AWSLambda.findUserMethodsImmediate(AWSLambda.java:125)
    at lambdainternal.AWSLambda.findUserMethods(AWSLambda.java:70)
    at lambdainternal.AWSLambda.startRuntime(AWSLambda.java:215)
    at lambdainternal.AWSLambda.<clinit>(AWSLambda.java:59)
    ... 3 more
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)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:114)
    ... 18 more

@offlinemark
Copy link

+1

@gatlingxyz
Copy link

Not sure if downgrading to 1.0.0 helped me, but for future searchers, adding this (from the sample project above) helped me:

jar {
    from {
        (configurations.runtime).collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }

    duplicatesStrategy DuplicatesStrategy.EXCLUDE
}

I also downgraded to 1.0.0, but doing that alone did not help.

@cbeattie-miovision
Copy link

I encountered the same problems as mentioned above but when I bundle the resources as a zip (as mentioned below) it worked for me on v1.1.0.

http://docs.aws.amazon.com/lambda/latest/dg/create-deployment-pkg-zip-java.html

@CoolersCoder
Copy link

CoolersCoder commented Jul 11, 2017

create zip.xml in your src/assembly/zip.xml.

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>lambda_deployment_package_assembly</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${project.build.directory}/classes</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>**/*.class</include>
            </includes>
        </fileSet>
    </fileSets>
    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
            <useProjectArtifact>false</useProjectArtifact>
        </dependencySet>
    </dependencySets>
</assembly>

In the pom file.

  <plugins>
        <plugin>
            <!--  Note: Normally, this plugin would be run using
            "mvn compile assembly:single" but the execution
            clause below binds it to the normal 'package' lifecycle phase
            -->
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptors>
                    <descriptor>src/assembly/lambda_deployment_package_assembly.xml</descriptor>
                </descriptors>
            </configuration>
            <executions>
                <execution>
                    <id>lambda_deployment_package_execution</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>

And then you good to go , it works for me. :)

@geethaRam
Copy link

Getting this error on v1.1.0. Will this be fixed?

@geethaRam
Copy link

Apparently, the maven-shade plugin was not running for goal:package. This solution solved for me: https://stackoverflow.com/questions/42208526/maven-shade-plugin-is-not-called-automatically-for-goal-package
Another way to validate if the jar contains all the dependencies is by running "jar tf .jar". This should list all depenent jars from com/amazonaws/services/lambda/runtime/events/*

@asuresh8
Copy link
Contributor

Glad you found your problem. Also, there a new version with more events that I would suggest you use.

jeromevdl added a commit to jeromevdl/aws-lambda-java-libs that referenced this issue Dec 4, 2020
jeromevdl added a commit to jeromevdl/aws-lambda-java-libs that referenced this issue Dec 10, 2020
carlzogh pushed a commit to jeromevdl/aws-lambda-java-libs that referenced this issue Dec 10, 2020
smirnoal pushed a commit that referenced this issue Jan 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants