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

Maven引入Project A jar包时出现NoSuchMethodError问题求助

Maven引入依赖Jar时出现NoSuchMethodError,项目依赖正常的解决方案

问题现象

将Project A作为项目依赖添加到Project B时,测试运行完全正常;但把Project A打包成Maven依赖Jar引入Project B后,抛出如下错误:

java.lang.NoSuchMethodError: com.sun.istack.localization.LocalizableMessageFactory.<init>(Ljava/lang/String;Lcom/sun/istack/localization/LocalizableMessageFactory$ResourceBundleSupplier;)V 
at com.sun.xml.ws.resources.StreamingMessages.<clinit>(StreamingMessages.java:28) 
at com.sun.xml.ws.util.FastInfosetUtil.<clinit>(FastInfosetUtil.java:35) 
at com.sun.xml.ws.encoding.SOAPBindingCodec.<init>(SOAPBindingCodec.java:153) 
at com.sun.xml.ws.encoding.SOAPBindingCodec.<init>(SOAPBindingCodec.java:134) 
at com.sun.xml.ws.api.pipe.Codecs.createSOAPBindingCodec(Codecs.java:43) 
at com.sun.xml.ws.api.message.MessageContextFactory.<init>(MessageContextFactory.java:66) 
at com.sun.xml.ws.api.message.MessageContextFactory.<init>(MessageContextFactory.java:52) 
at com.oracle.webservices.api.message.MessageContextFactory.<clinit>(MessageContextFactory.java:27) 
at com.sun.xml.ws.db.DatabindingImpl.<init>(DatabindingImpl.java:74) 
at com.sun.xml.ws.db.DatabindingProviderImpl.create(DatabindingProviderImpl.java:44) 
at com.sun.xml.ws.db.DatabindingProviderImpl.create(DatabindingProviderImpl.java:28) 
at com.sun.xml.ws.db.DatabindingFactoryImpl.createRuntime(DatabindingFactoryImpl.java:90) 
at com.sun.xml.ws.client.WSServiceDelegate.buildRuntimeModel(WSServiceDelegate.java:853) 
at com.sun.xml.ws.client.WSServiceDelegate.createSEIPortInfo(WSServiceDelegate.java:870) 
at com.sun.xml.ws.client.WSServiceDelegate.addSEI(WSServiceDelegate.java:833) 
at com.sun.xml.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:422) 
at com.sun.xml.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:391) 
at com.sun.xml.ws.client.WSServiceDelegate.getPort(WSServiceDelegate.java:373) 
at javax.xml.ws.Service.getPort(Service.java:119) 
at AssetMiscInfo.AssetMiscInfoWSv2.getAssetMiscInfoServicePort(AssetMiscInfoWSv2.java:75) 
at alfa.baseFunctions.API.AssetMiscInfoAPI.<init>(AssetMiscInfoAPI.java:42) 
at variables.SuiteBase.beforeTestMethod(SuiteBase.java:792) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:498) 
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:134) 
at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:63) 
at org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:348) 
at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:302) 
at org.testng.internal.TestInvoker.runConfigMethods(TestInvoker.java:695) 
at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:523) 
at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173) 
at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46) 
at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:816) 
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146) 
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146) 
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) 
at java.lang.Thread.run(Thread.java:748)

Project A的pom.xml配置

<dependencies>
    <dependency>
        <groupId>com.sun.xml.ws</groupId>
        <artifactId>jaxws-rt</artifactId>
        <version>2.3.5</version>
        
        <exclusions>
            <exclusion>
                <groupId>com.sun.istack</groupId>
                <artifactId>istack-commons-runtime</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <artifactId>sputnik</artifactId>
        <groupId>com.carmax.devops</groupId>
        <version>1.2.2</version>
    </dependency>
</dependencies>

<build>
    <sourceDirectory>utils.soap</sourceDirectory>
    <resources>
        <resource>
            <directory>utils.soap</directory>
            <includes>
                <include>**/*.java</include>
            </includes>
        </resource>
    </resources>

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-clean-plugin</artifactId>
            <version>3.1.0</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.1.0</version>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.8.1</version>
            <configuration>
                <verbose>true</verbose>
                <fork>true</fork>
                <excutable>${JAVA_HOME}/bin/javac</excutable>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

已尝试的无效操作

在pom.xml中添加以下排除配置,问题未解决:

<exclusions>
    <exclusion>
        <groupId>com.sun.istack</groupId>
        <artifactId>istack-commons-runtime</artifactId>
    </exclusion>
</exclusions>

解决方案

1. 定位依赖冲突根源

在Project B的根目录执行Maven命令,查看依赖树:

mvn dependency:tree -Dincludes=com.sun.istack:istack-commons-runtime

该命令会列出所有引入istack-commons-runtime的依赖,找到版本低于3.0.12的条目(jaxws-rt 2.3.5依赖的istack版本为3.0.12)。

2. 强制指定兼容版本

在Project B的pom.xml中直接引入正确版本的istack-commons-runtime,覆盖冲突版本:

<dependency>
    <groupId>com.sun.istack</groupId>
    <artifactId>istack-commons-runtime</artifactId>
    <version>3.0.12</version>
</dependency>

3. 调整Project A的依赖配置(可选)

移除Project A中对istack-commons-runtime的排除,确保依赖能正常传递:

<dependency>
    <groupId>com.sun.xml.ws</groupId>
    <artifactId>jaxws-rt</artifactId>
    <version>2.3.5</version>
    <!-- 移除以下排除块 -->
    <!-- <exclusions>
        <exclusion>
            <groupId>com.sun.istack</groupId>
            <artifactId>istack-commons-runtime</artifactId>
        </exclusion>
    </exclusions> -->
</dependency>

原因分析

  • 项目依赖模式下,Eclipse会自动合并所有模块的依赖,优先选择兼容的高版本;
  • Jar依赖模式下,Maven的依赖传递规则可能导致Project B引入了旧版本的istack-commons-runtime,而该版本中没有错误栈里提到的构造方法,从而抛出NoSuchMethodError。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 02:20:28