How do I resolve the "java.lang.ClassNotFoundException: com.example.package.resource.HandlerWrapper" error in AWS CloudFormation?

Last updated: 2020-08-17

I receive the "java.lang.ClassNotFoundException: com.example.package.resource.HandlerWrapper" error when I try to invoke my resource provider in AWS CloudFormation. How do I resolve this error?

Short description

You receive this error when you're developing or testing a resource provider schema and you call cfn test or sam local invoke, but the /target/ directory in your project doesn't contain a valid .jar file.

By default, the name of the .jar file is in the format organization-service-resource-handler-1.0-SNAPSHOT.jar and is configured in the pom.xml file in your project's root directory. For example:

<groupId>com.organization.service.resource</groupId>
<artifactId>organization-service-resource-handler</artifactId>
<name>organization-service-resource-handler</name>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

Resolution

1.    To build your project, run cfn generate followed by mvn package. For example:

$ cfn generate
Generated files for Organization::Service::Resource
$ mvn package
[INFO] Scanning for projects...
[INFO] 
[INFO] --< software.organization.service.resource:organization-service-resource-handler >--
[INFO] Building organization-service-resource-handler 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  21.690 s
[INFO] Finished at: 2020-07-14T16:02:47-05:00
[INFO] ------------------------------------------------------------------------

Note: If the build and tests are successful, mvn creates the necessary .jar file in the /target/ directory.

2.    If your unit tests aren't complete and you want to skip the tests, run mvn -Dmaven.test.skip=true package instead of mvn package.

$ cfn generate
Generated files for Organization::Service::Resource
$ mvn package -Dmaven.test.skip=true package
[INFO] Scanning for projects...
[INFO] 
[INFO] --< software.organization.service.resource:organization-service-resource-handler >--
[INFO] Building organization-service-resource-handler 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  21.690 s
[INFO] Finished at: 2020-07-14T16:02:47-05:00
[INFO] ------------------------------------------------------------------------

Tip: You can troubleshoot the tests by navigating to the directory /target/surefire-reports from your project's root directory.


Did this article help?


Do you need billing or technical support?