aws-sam-cli icon indicating copy to clipboard operation
aws-sam-cli copied to clipboard

sam invoke not working for java8. Getting class not found exception!

Open debabratapatra opened this issue 7 years ago • 5 comments

I am getting Class not found exception while running sam invoke.

I ran below commands.

sam init --runtime java8 cd sam-app sam build sam local invoke "HelloWorldFunction" -e test.json

I got below error.

image

I am using window 7 OS. Please let me know if I am missing something.

Thanks in advance!

debabratapatra avatar Oct 16 '19 09:10 debabratapatra

See #1436. This is a docker problem.

jfuss avatar Oct 17 '19 15:10 jfuss

Thanks.

debabratapatra avatar Oct 18 '19 06:10 debabratapatra

Hi - this appears to still be an issue on Mac, and I've got my project (or a parent) shared as a volume via Docker Desktop. I can even see my folder being mounted in the log:

❯ sam local invoke -t template.yaml -e events/event.json
Invoking HelloWorld.Handler::handleRequest (java11)
libs is a local Layer in the template
Building image........................
Skip pulling image and use local one: samcli/lambda:java11-arm64-739a304e183a900711f6497e6.

Mounting /Users/localuser/repos/project/HelloWorld/HelloWorldFunction as /var/task:ro,delegated inside runtime container
START RequestId: 0f43f7f4-38c8-49ec-9b9d-03e14bfdee4e Version: $LATEST
Class not found: HelloWorld.Handler: java.lang.ClassNotFoundException
java.lang.ClassNotFoundException: HelloWorld.Handler. Current classpath: file:/var/task/

END RequestId: 0f43f7f4-38c8-49ec-9b9d-03e14bfdee4e
REPORT RequestId: 0f43f7f4-38c8-49ec-9b9d-03e14bfdee4e	Init Duration: 0.21 ms	Duration: 238.76 ms	Billed Duration: 239 ms	Memory Size: 512 MB	Max Memory Used: 512 MB
{"errorMessage":"Class not found: HelloWorld.Handler","errorType":"java.lang.ClassNotFoundException"}%

My project layout:

❯ tree
.
├── README.md
├── events
│   └── event.json
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── layers
│   └── build.gradle
├── out.json
├── HelloWorldFunction
│   ├── build.gradle
│   └── src
│       ├── main
│       │   └── java
│       │       └── HelloWorld
│       │           └── Handler.java
├── samconfig.toml
├── settings.gradle
└── template.yaml

16 directories, 22 files

shearn89 avatar Feb 23 '22 14:02 shearn89

Huh - looks like removing the -t template.yaml argument changed the error and it looks like it's running okay (or at least failing elsewhere).

shearn89 avatar Feb 23 '22 14:02 shearn89

Had this problem with maven and only when running remotely on AWS and thanks to https://youtu.be/Q14uFOshVa8?t=165 all it took to work was to run mvn clean package before sam build and change the CodeUri of the sam template to CodeUri: HelloWorldFunction/target/HelloWorld-1.0.jar Still, does not fill right that we have to do this extra steps that could be done by generic sam cli calls.

macaranduba avatar Feb 01 '23 10:02 macaranduba