jar包加载的位置顺序

admin2024-04-03  3



一个基于SpringBoot的 Flink程序(FlinkListener.jar),在Windows 8系统 和 在自己的Linux虚拟机上运行正常, 把 jar 包上传到局域网Linux服务器上,提示 NoSuchMethodError错误
Caused by: java.lang.NoSuchMethodError: com.mysql.cj.CharsetMapping.getJavaEncodingForMysqlCharset(Ljava/lang/String;)Ljava/lang/String;
        at io.debezium.connector.mysql.MySqlValueConverters.charsetFor(MySqlValueConverters.java:331) ~[debezium-connector-mysql-1.5.4.Final.jar!/:1.5.4.Final]
        at io.debezium.connector.mysql.MySqlValueConverters.converter(MySqlValueConverters.java:298) ~[debezium-connector-mysql-1.5.4.Final.jar!/:1.5.4.Final]
        at io.debezium.relational.TableSchemaBuilder.createValueConverterFor(TableSchemaBuilder.java:400) ~[debezium-core-1.5.4.Final.jar!/:1.5.4.Final]
        at io.debezium.relational.TableSchemaBuilder.convertersForColumns(TableSchemaBuilder.java:321) ~[debezium-core-1.5.4.Final.jar!/:1.5.4.Final]
        at io.debezium.relational.TableSchemaBuilder.createValueGenerator(TableSchemaBuilder.java:246) ~[debezium-core-1.5.4.Final.jar!/:1.5.4.Final]
        at io.debezium.relational.TableSchemaBuilder.create(TableSchemaBuilder.java:138) ~[debezium-core-1.5.4.Final.jar!/:1.5.4.Final]
        at io.debezium.relational.RelationalDatabaseSchema.buildAndRegisterSchema(RelationalDatabaseSchema.java:130) ~[debezium-core-1.5.4.Final.jar!/:1.5.4.Final]
        at io.debezium.relational.HistorizedRelationalDatabaseSchema.recover(HistorizedRelationalDatabaseSchema.java:52) ~[debezium-core-1.5.4.Final.jar!/:1.5.4.Final]
        at com.ververica.cdc.connectors.mysql.debezium.task.context.StatefulTaskContext.validateAndLoadDatabaseHistory(StatefulTaskContext.java:163) ~[flink-connector-mysql-cdc-2.2.1.jar!/:2.2.1]
        at com.ververica.cdc.connectors.mysql.debezium.task.context.StatefulTaskContext.configure(StatefulTaskContext.java:114) ~[flink-connector-mysql-cdc-2.2.1.jar!/:2.2.1]
        at com.ververica.cdc.connectors.mysql.debezium.reader.SnapshotSplitReader.submitSplit(SnapshotSplitReader.java:93) ~[flink-connector-mysql-cdc-2.2.1.jar!/:2.2.1]
        at com.ververica.cdc.connectors.mysql.debezium.reader.SnapshotSplitReader.submitSplit(SnapshotSplitReader.java:64) ~[flink-connector-mysql-cdc-2.2.1.jar!/:2.2.1]
        at com.ververica.cdc.connectors.mysql.source.reader.MySqlSplitReader.checkSplitOrStartNext(MySqlSplitReader.java:163) ~[flink-connector-mysql-cdc-2.2.1.jar!/:2.2.1]
        at com.ververica.cdc.connectors.mysql.source.reader.MySqlSplitReader.fetch(MySqlSplitReader.java:73) ~[flink-connector-mysql-cdc-2.2.1.jar!/:2.2.1]
        at org.apache.flink.connector.base.source.reader.fetcher.FetchTask.run(FetchTask.java:56) ~[flink-connector-base-1.13.3.jar!/:1.13.3]
        at org.apache.flink.connector.base.source.reader.fetcher.SplitFetcher.runOnce(SplitFetcher.java:140) ~[flink-connector-base-1.13.3.jar!/:1.13.3]
        at org.apache.flink.connector.base.source.reader.fetcher.SplitFetcher.run(SplitFetcher.java:103) ~[flink-connector-base-1.13.3.jar!/:1.13.3]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[na:1.8.0_151]

查看 FlinkListener.jar 包中的lib目录下是否有相关mysql依赖

[root@jenkinspc25 tools]# jar -tvf FlinkListener.jar | grep mysql.*.jar
251194 Tue Apr 26 19:08:40 CST 2022 BOOT-INF/lib/flink-connector-mysql-cdc-2.2.1.jar
370666 Thu Jul 01 01:03:16 CST 2021 BOOT-INF/lib/debezium-connector-mysql-1.5.4.Final.jar
192762 Tue Apr 20 16:13:28 CST 2021 BOOT-INF/lib/mysql-binlog-connector-java-0.25.1.jar
2397321 Tue Jun 16 17:17:36 CST 2020 BOOT-INF/lib/mysql-connector-java-8.0.21.jar

而报错提示的 CharsetMapping 类在 mysql-connector-java-8.0.21.jar 依赖中.


使用 -XX:+TraceClassLoading 查看类的加载情况
java -XX:+TraceClassLoading -jar FlinkListener.jar > FlinkListener.log &

查看 FlinkListener.log 文件, 定位到 CharsetMapping 关键字

jar包加载的位置顺序,在这里插入图片描述,第1张CharsetMapping 这个类是从 /home/java/jre/lib/ext/mysql-connector-java-8.0.27.jar 加载的, 并不是从 FlinkListener.jar!/BOOT-INF/lib 目录下加载 .

天真的以为, 把 /home/java/jre/lib/ext 目录下的 mysql-connector-java-8.0.27.jar 名称改成 mysql-connector-java-8.0.27.jar.remove 就可以, 哪知道它依然会从 mysql-connector-java-8.0.27.jar.remove 加载,如下所示
jar包加载的位置顺序,在这里插入图片描述,第2张通过 file 命令查看, 它的确还是JAR类型 (不能以貌取人,不能以貌判断一个文件的类型)

[root@localhost ext]# file mysql-connector-java-8.0.27.jar.remove 
mysql-connector-java-8.0.27.jar.remove: Java archive data (JAR)

【解决方案】
1.直接移除 /home/java/jre/lib/ext 目录下的 mysql-connector-java-8.0.27.jar 包. 但可能会影响其它的程序,未采用此方案
2.其他方案 待定

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明原文出处。如若内容造成侵权/违法违规/事实不符,请联系SD编程学习网:675289112@qq.com进行投诉反馈,一经查实,立即删除!