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

2 minute read
0

When I try to invoke my resource provider in AWS CloudFormation, I receive the following error: "java.lang.ClassNotFoundException: com.example.package.resource.HandlerWrapper"

Resolution

When you develop or test a resource type schema file, you call the cfn test or sam local invoke. If the /target/ directory in your project doesn't contain a valid .jar file, then you receive the java.lang.ClassNotFoundException: com.example.package.resource.HandlerWrapper error.

By default, the .jar file uses the organization-service-resource-handler-1.0-SNAPSHOT.jar format and is configured in the pom.xml file in your project's root directory. 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>

To build your project, run cfn generate and then run mvn package:

cfn generateGenerated 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] ------------------------------------------------------------------------

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

To skip the tests for unit tests that aren't complete, run mvn -Dmaven.test.skip=true package instead of mvn package:

cfn generateGenerated 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] ------------------------------------------------------------------------

Note: To troubleshoot the tests, open the /target/surefire-reports directory from your project's root directory.

For other errors that are related to resource providers, see the following articles:

Related information

CloudFormation CLI on the GitHub website

AWS OFFICIAL
AWS OFFICIALUpdated 6 months ago