项目场景:
提示:halo 插件开发过程中 版本2.21
在halo插件开发中运行main方法直接报错
问题描述
例如:运行一个main方法时
public static void main(String[] args) {
System.out.println("1111");
}
报错如下
> Task :WeReadApiClient.main() FAILED
错误: 无法初始化主类 run.halo.weread.util.WeReadApiClient
原因: java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/JsonNode
Execution failed for task ':WeReadApiClient.main()'.
> Process 'command '/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.
Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
For more on this, please refer to https://docs.gradle.org/8.9/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
BUILD FAILED in 6s
7 actionable tasks: 4 executed, 3 up-to-date
Execution failed for task ':WeReadApiClient.main()'.
> Process 'command '/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.
Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
For more on this, please refer to https://docs.gradle.org/8.9/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
BUILD FAILED in 4s
7 actionable tasks: 3 executed, 4 up-to-date
原因分析:
❓ 为什么添加这个依赖就能解决问题?
你在代码中使用了 Jackson 的核心类,例如:
com.fasterxml.jackson.databind.JsonNode
ObjectMapper
或者涉及 JSON 序列化/反序列化的操作
这些功能都属于 Jackson Databind 模块。
但你在最初没有显式声明该依赖,导致运行时抛出异常:
java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/JsonNode
这表示 JVM 在运行时找不到 JsonNode 这个类,不是编译期找不到(否则 Gradle 编译就会失败),而是 运行时 classpath 中缺少该类所在的 JAR 包。
🧩 Jackson 核心模块说明
模块 | 功能 | 是否需要| |
---|---|---|
jackson-core | 核心流处理 API(如 JsonParser, JsonGenerator) | ✅ 自动被 databind 引入 |
jackson-annotations | 注解支持(如 @JsonProperty) | ✅ 自动被 databind 引入 |
jackson-databind | 数据绑定,提供 ObjectMapper 和 JsonNode 等高级 API | ⚠️ 必须手动引入 |
⚠️ jackson-databind 不会自动通过平台依赖或 Halo 插件引入,必须在你的项目中显式声明。
解决方案:
提示:在build.gradle 中添加 Jackson 的运行时依赖
在 build.gradle 中添加 Jackson 的运行时依赖。
添加 Jackson 依赖
在 dependencies 块中添加以下内容:
implementation 'com.fasterxml.jackson.core:jackson-databind:2.16.1'
完整示例如下:
dependencies {
implementation 'org.apache.httpcomponents:httpclient:4.5.13'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.16.1'
implementation platform('run.halo.tools.platform:plugin:2.20.0-SNAPSHOT')
compileOnly 'run.halo.app:api'
testImplementation 'run.halo.app:api'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
评论区