aws / serverless-java-container

A Java wrapper to run Spring, Spring Boot, Jersey, and other apps inside AWS Lambda.
https://aws.amazon.com/serverless/
Apache License 2.0
1.5k stars 561 forks source link

Class not found: com.madman.lambda.StreamLambdaHandler: java.lang.ClassNotFoundException #354

Open anudina opened 4 years ago

anudina commented 4 years ago

when I invoke Lambda for Petstore using postman or using test feature in lambda console in

URL invoked: https://hostnameXXXXXXX/lambda/petstore

AWS I get below error "START RequestId: 78c5fb37-4f7e-43e9-a9aa-e4266eb5711b Version: $LATEST Class not found: com.madman.lambda.StreamLambdaHandler: java.lang.ClassNotFoundException java.lang.ClassNotFoundException: com.madman.lambda.StreamLambdaHandler 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)

END RequestId: 78c5fb37-4f7e-43e9-a9aa-e4266eb5711b"

Expected behavior ; It should work and provide me pets

Actual behavior : throwing error

Steps to reproduce : deploy petstore app for springboot2 in aws lambda and invoke GET operation using ALB trigger or API trigger

Full log output

2020-06-11T00:22:25.310-04:00 START RequestId: 78c5fb37-4f7e-43e9-a9aa-e4266eb5711b Version: $LATEST
  2020-06-11T00:22:25.324-04:00 Class not found: com.madman.lambda.StreamLambdaHandler: java.lang.ClassNotFoundExceptionjava.lang.ClassNotFoundException: com.madman.lambda.StreamLambdaHandler 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)
  2020-06-11T00:22:25.328-04:00 END RequestId: 78c5fb37-4f7e-43e9-a9aa-e4266eb5711b
  2020-06-11T00:22:25.328-04:00 REPORT RequestId: 78c5fb37-4f7e-43e9-a9aa-e4266eb5711b Duration: 18.20 ms Billed Duration: 100 ms Memory Size: 256 MB Max Memory Used: 75 MB Init Duration: 114.62 ms

image

image

sapessi commented 4 years ago

How did you build and package the application?

softwareguidancechintala commented 4 years ago

@sapessi and @anudina

I had the same issue.

I did a "gradle clean build" on aws-serverless-java-container-master\samples\springboot2\pet-store

And then referred "build\libs\pet-store.jar" in the CodeUri attribute of the template.yml

`AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: Example Pet Store API written with SpringBoot with the aws-serverless-java-container library

Globals: Api:

API Gateway regional endpoints

EndpointConfiguration: REGIONAL

Resources: PetStoreFunction: Type: AWS::Serverless::Function Properties: Handler: com.amazonaws.serverless.sample.springboot2.StreamLambdaHandler::handleRequest Runtime: java8 CodeUri: build\libs\pet-store.jar MemorySize: 1512 Policies: AWSLambdaBasicExecutionRole Timeout: 60 Events: HttpApiEvent: Type: HttpApi Properties: TimeoutInMillis: 20000 PayloadFormatVersion: '1.0'

Outputs: SpringBootPetStoreApi: Description: URL for application Value: !Sub 'https://${ServerlessHttpApi}.execute-api.${AWS::Region}.amazonaws.com/pets' Export: Name: SpringBootPetStoreApi `

Once I deployed, I got 502 Bad Gateway while testing the API endpoint.

When I looked into CloudWatch logs, I found

Class not found: com.amazonaws.serverless.sample.springboot2.StreamLambdaHandler: java.lang.ClassNotFoundException java.lang.ClassNotFoundException: com.amazonaws.serverless.sample.springboot2.StreamLambdaHandler 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)

Can you please advise on this.

Spring boot version: 2.2.6.RELEASE Gradle : 6.3

laurentgrangeau commented 4 years ago

I have the exact same issue when building and deploying the springboot2 sample application. Do you have any updates about this problem ?

SimerSingh commented 3 years ago

I am facing same issue , already spent lot of time looking for solution. Anyone solved this?

ruliyeret commented 3 years ago

I have the same issue updates?

sos418 commented 3 years ago

In template.yml file:

CodeUri according to maven or gradle setting.

I use maven and change to "./target/my-service-1.0-SNAPSHOT-lambda-package.zip"

截圖 2021-06-12 上午10 38 05
alpasarupria commented 3 years ago

Facing the same. can someone give solution to this issue

deki commented 3 years ago

Hi, there is no need to modify the CodeUri manually.

Just follow the steps as described in the readme: https://github.com/awslabs/aws-serverless-java-container/blob/master/samples/springboot2/pet-store/README.md

I just tried it with master and dev branch and it was working without any issues.

alpasarupria commented 3 years ago

Thank you for your answer. Can you provide me any link where Cloudformation is not used. Is there any way where in I can do through terraform or UI console

deki commented 3 years ago

@alpasarupria you can also do it without SAM and CloudFormation. I'm not a Terraform expert but looking at https://github.com/hashicorp/terraform/issues/8344 Terraform allows to zip AWS Lambda functions on the fly. The documentation is here: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function

The UI also supports upload from ZIP https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-package.html#gettingstarted-package-zip but I would recommend to use some sort of tool support.

hopewise commented 1 year ago

in the root of the project, you need to run:

sam build -u

before you can run:

 sam local start-api

and test in browser:

127.0.0.1:3000/pets
timhughes commented 11 months ago

Leaving a comment for future me. This turned out to be a selinux issue https://github.com/aws/aws-sam-cli/issues/2360