「Apache Groovy」- java.lang.NoSuchMethodError: x.x.x: method <init>()V not found @20210223

本文探讨了在Groovy中遇到的‘<init>()V not found’错误,重点介绍了未调用父类构造函数导致的问题,以及如何通过调用相同构造函数或使用InheritConstructors注解来修复。实例代码演示了两种解决方案并给出了相关API参考。

问题描述

在执行 Groovy 代码中,产生如下错误:

ava.lang.NoSuchMethodError: com.lispstudio.model.TeamLispstudio: method <init>()V not found

问题原因

在继承父类之后,没调用父类的构造函数。

解决方法

有两种解决方法:1)调用与父类相同的构造函数;2)使用 InheritConstructors 注解;

调用与父类相同的构造函数

class Creature {
    Creature (String feature) {}
}

class Humman extends Creature{
    Humman (String feature) {
		super(feature)
    }
}

new Humman("legs")

使用 InheritConstructors 注解

class Creature {
    Creature (String feature) {}
}

@groovy.transform.InheritConstructors
class Humman extends Creature{
}

new Humman("legs")

相关文章

「Groovy」- Unable to load extension class

参考文献

<init>()V not found when defining superclass constructor
InheritConstructors (groovy 2.4.9 API)

<think>我们正在处理一个与ShardingSphere相关的NoSuchMethodError异常,具体涉及Representer类的初始化方法缺失。这个问题通常发生在使用YAML配置时,可能是由于依赖库版本不兼容导致的。 根据用户描述,错误信息指向Representer类的初始化方法(init method)找不到。Representer类是SnakeYAML库中的一部分,用于YAML的序列化和反序列化。ShardingSphere在解析YAML配置文件时会使用到这个库。 可能的原因: 1. 项目中引入了不兼容的SnakeYAML版本。ShardingSphere依赖的SnakeYAML版本与项目中实际使用的版本不一致。 2. 依赖冲突:可能存在多个版本的SnakeYAML,导致类加载器加载了错误的版本。 解决方案步骤: 1. **检查依赖树,确认SnakeYAML版本** 使用Maven或Gradle检查依赖树,查找SnakeYAML的版本。确保使用的版本与ShardingSphere要求的版本一致。 - Maven: `mvn dependency:tree` - Gradle: `gradle dependencies` 2. **排除冲突的依赖** 如果发现多个版本的SnakeYAML,需要排除掉不需要的版本。例如,在Maven中,可以在依赖声明中排除: ```xml <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>shardingsphere-jdbc-core</artifactId> <version>${shardingsphere.version}</version> <exclusions> <exclusion> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId> </exclusion> </exclusions> </dependency> ``` 然后显式引入正确的版本: ```xml <dependency> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId> <version>1.33</version> <!-- 请根据ShardingSphere要求版本填写 --> </dependency> ``` 3. **检查ShardingSphere版本对应的SnakeYAML版本** 查阅ShardingSphere官方文档或源码,确定当前使用的ShardingSphere版本所依赖的SnakeYAML版本。例如,ShardingSphere 5.3.0要求SnakeYAML版本为1.30+,但推荐使用1.33。 4. **清理和重新构建项目** 在排除依赖并重新引入后,清理项目并重新构建(如使用`mvn clean install`或`gradle clean build`),确保更改生效。 5. **运行时检查** 如果问题仍然存在,可以在运行时通过以下代码检查实际加载的SnakeYAML版本: ```java System.out.println(org.yaml.snakeyaml.Yaml.class.getProtectionDomain().getCodeSource().getLocation()); ``` 6. **使用ShardingSphere提供的依赖管理** 如果使用Maven,可以考虑使用ShardingSphere的BOM(Bill of Materials)来统一管理版本: ```xml <dependencyManagement> <dependencies> <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>shardingsphere-dependencies</artifactId> <version>${shardingsphere.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> ``` 7. **检查其他依赖冲突** 如果上述步骤不能解决问题,可能还有其他依赖冲突。可以使用工具(如maven helper)来进一步分析。 注意:在升级或降级SnakeYAML版本时,需要注意版本之间的兼容性,避免引入新的问题。 如果用户按照上述步骤操作后问题仍然存在,建议提供具体的错误堆栈信息以及项目依赖树,以便进一步分析。 引用说明:ShardingSphere官方文档中关于依赖管理的部分[^1]。 §§相关问题§§ 1. 如何查看项目中实际加载的依赖版本? 2. ShardingSphere 5.3.0依赖的第三方库版本有哪些? 3. 除了SnakeYAML,还有哪些常见原因会导致NoSuchMethodError? 4. 如何避免Java项目中的依赖冲突?</think>### ShardingSphere NoSuchMethodError: Representer init方法缺失解决方案 此问题通常由 **SnakeYAML库版本冲突** 引起,具体表现为`org.yaml.snakeyaml.representer.Representer`类初始化方法缺失。以下是系统化解决步骤: --- ### 原因分析 1. **依赖冲突** ShardingSphere依赖SnakeYAML解析YAML配置,当项目中存在多个冲突版本时,会加载错误的`Representer`类实现[^1]。 ```mermaid graph LR A[项目依赖] --> B[ShardingSphere] A --> C[其他库] B --> D[snakeyaml-1.30] C --> E[snakeyaml-1.18] // 冲突版本 ``` 2. **版本不兼容** ShardingSphere 5.x+ 要求 SnakeYAML ≥1.30,旧版缺少`Representer`类的初始化方法。 --- ### 解决步骤 #### 1. 检查依赖树 ```bash # Maven mvn dependency:tree | grep snakeyaml # Gradle gradle dependencies | grep snakeyaml ``` - 若输出多个版本(如1.18和1.33),需解决冲突 #### 2. 强制统一版本(Maven示例) ```xml <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>shardingsphere-jdbc-core</artifactId> <version>5.3.2</version> <exclusions> <!-- 排除旧版 --> <exclusion> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId> </exclusion> </exclusions> </dependency> <!-- 显式引入兼容版本 --> <dependency> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId> <version>1.33</version> <!-- 推荐版本 --> </dependency> ``` #### 3. Gradle解决方案 ```groovy configurations.all { resolutionStrategy.force 'org.yaml:snakeyaml:1.33' } ``` #### 4. 验证修复 启动时检查加载的JAR路径: ```java public static void main(String[] args) { System.out.println(Representer.class.getProtectionDomain() .getCodeSource().getLocation()); // 应输出包含1.33的路径 } ``` --- ### 预防措施 1. **使用BOM管理版本** ```xml <dependencyManagement> <dependencies> <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>shardingsphere-dependencies</artifactId> <version>5.3.2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> ``` 2. **定期清理无效依赖** ```bash mvn dependency:purge-local-repository ``` --- ### 补充说明 若仍出现错误,需检查: - 依赖作用域(如`<scope>provided</scope>`是否被覆盖) - 类加载器隔离问题(尤其在容器环境中) - ProGuard/R8混淆规则(移动端场景) > 引用说明:ShardingSphere官方文档中明确标注了YAML依赖的版本要求[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值