You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Apache NiFi中使用tnm4j库遇NoSuchMethodError问题求助

Apache NiFi中使用tnm4j执行walk操作时出现NoSuchMethodError问题

问题详情

在Apache NiFi环境中使用tnm4j库执行SNMP walk操作时,抛出以下错误:

java.lang.NoSuchMethodError: 'void net.percederberg.mibble.asn1.Asn1Parser.reset(java.io.Reader, net.percederberg.grammatica.parser.Analyzer)' 
at net.percederberg.mibble.MibLoader$MibSource.parseMib(MibLoader.java:970) 
at net.percederberg.mibble.MibLoader.loadQueue(MibLoader.java:724) 
at net.percederberg.mibble.MibLoader.load(MibLoader.java:550) 
at net.percederberg.mibble.MibLoader.load(MibLoader.java:451) 
at org.soulwing.snmp.provider.mibble.CachingMibRepository.load(CachingMibRepository.java:79) 
at org.soulwing.snmp.provider.mibble.MibbleMib.load(MibbleMib.java:226)

该问题仅在NiFi环境中出现,普通Java项目可正常运行。项目Pom.xml配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>com.pnm</groupId>
    <artifactId>pnm</artifactId>
    <version>1.0</version>
</parent>

<artifactId>nifi-PNMtest-processors</artifactId>
<packaging>jar</packaging>

<dependencies>

    <dependency>
        <groupId>org.soulwing.snmp</groupId>
        <artifactId>tnm4j</artifactId>
        <version>1.2.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.nifi</groupId>
        <artifactId>nifi-api</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.nifi</groupId>
        <artifactId>nifi-utils</artifactId>
        <version>1.20.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.nifi</groupId>
        <artifactId>nifi-mock</artifactId>
        <version>1.20.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

解决方案

1. 确认依赖冲突

这个错误本质是类版本冲突:NiFi环境中存在旧版本的mibble或grammatica库,与tnm4j依赖的版本不兼容。

  • 执行mvn dependency:tree查看项目依赖树,确认tnm4j依赖的net.percederberg.mibble:mibble和net.percederberg.grammatica:grammatica版本(tnm4j 1.2.0对应mibble 2.9.3、grammatica 1.6.1)。
  • 检查NiFi安装目录下的lib文件夹,是否存在同名但版本更低的jar包。

2. 强制指定依赖版本

在Pom.xml中添加dependencyManagement节点,强制使用tnm4j兼容的版本:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>net.percederberg.mibble</groupId>
            <artifactId>mibble</artifactId>
            <version>2.9.3</version>
        </dependency>
        <dependency>
            <groupId>net.percederberg.grammatica</groupId>
            <artifactId>grammatica</artifactId>
            <version>1.6.1</version>
        </dependency>
    </dependencies>
</dependencyManagement>

3. 使用Shade插件重命名依赖包(推荐)

由于NiFi的类加载器会优先加载自身lib中的jar,最稳妥的方式是将tnm4j及其依赖打包进自定义处理器jar,并重命名包路径,避免冲突:
在Pom.xml的build节点下添加maven-shade-plugin配置:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.4.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <relocations>
                            <relocation>
                                <pattern>net.percederberg</pattern>
                                <shadedPattern>com.pnm.shaded.net.percederberg</shadedPattern>
                            </relocation>
                        </relocations>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

替换com.pnm.shaded为你项目的自定义前缀,打包后将生成的shaded jar部署到NiFi即可。

内容的提问来源于stack exchange,提问作者Faisal Khan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 13:27:07