# java.lang.ClassNotFoundException: com.example.MyHandler : 从 SnapStart 快照加载类时出错

- **ID:** `aws/lambda-snapstart-java-class-not-found`
- **领域:** aws
- **类别:** runtime_error
- **错误码:** `ClassNotFoundException`
- **验证级别:** ai_generated
- **修复率:** 78%

## 根因

Lambda 函数的处理程序类不在 Java 类路径中，或者 SnapStart 快照是在不同的类路径配置下构建的，导致恢复期间不匹配。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| AWS Lambda Java 11 runtime | active | — | — |
| AWS Lambda Java 17 runtime | active | — | — |
| SnapStart (2023-04-01) | active | — | — |

## 解决方案

1. ```
   验证处理程序类是否在部署包中：`jar tf my-function.jar | grep MyHandler`。如果缺失，使用正确的类路径重新构建。然后将 Lambda 处理程序更新为完全限定的类名（例如 'com.example.MyHandler::handleRequest'）。
   ```
2. ```
   禁用 SnapStart 并重新部署：`aws lambda update-function-configuration --function-name my-function --snap-start ApplyOn=None`。确认函数正常工作后，使用新的快照重新启用 SnapStart。
   ```
3. ```
   使用 Maven Shade Plugin 或 Gradle Shadow 等构建工具创建包含所有依赖项和正确处理程序类的 fat JAR，然后启用 SnapStart 进行部署。
   ```

## 无效尝试

- **** — Rebuilding the deployment package without cleaning the build cache may include stale class files that don't match the SnapStart snapshot. (55% 失败率)
- **** — Adding the class to the system classpath via environment variables (e.g., CLASSPATH) is ignored by SnapStart because it uses a pre-initialized JVM. (70% 失败率)
- **** — Assuming it's a runtime dependency issue and adding all JARs to the deployment package, when the real issue is a handler class name typo. (45% 失败率)
