在linux执行java程序报错:Exception in thread "main" java.lang.NoSuchMethodError:

环境

IDEA Ultimate 2018.3
MAVEN 3.5.4

背景

将maven工程打包成jar(不包含第三方jar包),放到linux主机上执行,指定第三方jar路径,命令如下:

nohup java -Djava.ext.dirs=/usr/java/jdk1.8.0_181-cloudera/jre/lib/ext:/dev/shm/testMetrics/lib -jar sink-1.0-SNAPSHOT-jar-with-dependencies.jar > temp.txt 2>&1 &

其中第三方jar放置在/dev/shm/testMetrics/lib文件夹下,运行报错:

[2019-04-29 14:16:21]-[main-DEBUG]-[org.apache.hadoop.util.Shell-isSetsidSupported(822)]: setsid exited with exit code 0
 Exception in thread "main" java.lang.NoSuchMethodError: org.apache.commons.beanutils.PropertyUtilsBean.addBeanIntrospector(Lorg/apache/commons/beanutils/BeanIntrospector;)V
        at org.apache.commons.configuration2.beanutils.BeanHelper.initBeanUtilsBean(BeanHelper.java:631)
        at org.apache.commons.configuration2.beanutils.BeanHelper.<clinit>(BeanHelper.java:89)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:264)
        at com.sun.proxy.$Proxy1.<clinit>(Unknown Source)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
        at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:739)
        at org.apache.commons.configuration2.builder.fluent.Parameters.createParametersProxy(Parameters.java:294)
        at org.apache.commons.configuration2.builder.fluent.Parameters.fileBased(Parameters.java:185)
        at org.apache.commons.configuration2.builder.fluent.Configurations.fileParams(Configurations.java:602)
        at org.apache.commons.configuration2.builder.fluent.Configurations.fileParams(Configurations.java:638)
        at org.apache.commons.configuration2.builder.fluent.Configurations.fileBasedBuilder(Configurations.java:164)
        at org.apache.commons.configuration2.builder.fluent.Configurations.propertiesBuilder(Configurations.java:264)
        at com.ucar.spacex.metrics.impl.MetricsConfig.loadFirst(MetricsConfig.java:98)
        at com.ucar.spacex.metrics.impl.MetricsConfig.create(MetricsConfig.java:81)
        at com.ucar.spacex.metrics.impl.MetricsSystemImpl.configure(MetricsSystemImpl.java:454)
        at com.ucar.spacex.metrics.impl.MetricsSystemImpl.start(MetricsSystemImpl.java:158)
        at com.ucar.spacex.metrics.impl.MetricsSystemImpl.init(MetricsSystemImpl.java:129)
        at com.ucar.spacex.metrics.lib.DefaultMetricsSystem.init(DefaultMetricsSystem.java:41)
        at com.ucar.spacex.metrics.lib.DefaultMetricsSystem.initialize(DefaultMetricsSystem.java:37)
        at com.ucar.spacex.metrics.CommonMetricsSystem.main(CommonMetricsSystem.java:17)

分析

我去查看了org.apache.commons.beanutils.PropertyUtilsBean这个类所在的jar包,发现这个类同时存在于两个jar包的不同版本中

mvn dependency:tree -Dverbose -Dincludes=*commons*:*beanutils*

[WARNING] Using Maven 2 dependency tree to get verbose output, which may be inconsistent with actual Maven 3 resolution
[INFO] com.ucar.spacex:sink:jar:1.0-SNAPSHOT
[INFO] +- org.apache.hadoop:hadoop-common:jar:3.1.0:provided
[INFO] |  \- commons-beanutils:commons-beanutils:jar:1.9.3:provided
[INFO] \- commons-configuration:commons-configuration:jar:1.6:provided
[INFO]    +- commons-digester:commons-digester:jar:1.8:provided
[INFO]    |  \- (commons-beanutils:commons-beanutils:jar:1.7.0:provided - omitted for conflict with 1.9.3)
[INFO]    \- commons-beanutils:commons-beanutils-core:jar:1.8.0:provided

将pom.xml中依赖commons-configuration:commons-configuration:jar:1.6删除后重新打包解决问题。

参考链接

1、java -jar命令运行jar包时指定外部依赖jar包
2、maven将依赖包一起打入jar包中

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页