java.lang.NoSuchFieldError:bitpay SDK中的INSTANCE

Pet*_*zov 11 java maven maven-dependency

我想实现这个代码

public void testGetExchangeRate() throws Exception
{
    ECKey key = KeyUtils.createEcKey();

    String clientName = "server 1";
    BitPay bitpay = new BitPay(key, clientName);

    if (!bitpay.clientIsAuthorized(BitPay.FACADE_MERCHANT))
    {
        // Get Merchant facade authorization code
        String pairingCode = bitpay.requestClientAuthorization(
            BitPay.FACADE_MERCHANT);

        // Signal the device operator that this client needs to
        // be paired with a merchant account.
        System.out.print("Info: Pair this client with your merchant account using the pairing Code: " + pairingCode);
        throw new BitPayException("Error:client is not authorized for Merchant facade");
    }
}
Run Code Online (Sandbox Code Playgroud)

我包含了这些依赖项:

<dependency>
    <groupId>com.github.bitpay</groupId>
    <artifactId>java-bitpay-client</artifactId>
    <version>v2.0.4</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore</artifactId>
    <version>4.4.5</version>
    <type>jar</type>
</dependency>
Run Code Online (Sandbox Code Playgroud)

但是当我运行代码时,我得到:

testGetExchangeRate(com.payment.gateway.bitpay.impl.BitpayImplTest)  Time elapsed: 1.665 sec  <<< ERROR!
java.lang.NoSuchFieldError: INSTANCE
    at com.payment.gateway.bitpay.impl.BitpayImplTest.testGetExchangeRate(BitpayImplTest.java:55)
Run Code Online (Sandbox Code Playgroud)

问题:您能否就我如何解决这个问题提出一些建议?

A_D*_*teo 1

查看githubpom.xml上的库项目文件的maven依赖,虽然不是同一个artifact版本,但是可以看到依赖了几个库:java-bitpay-clientorg.apache.httpcomponents

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>fluent-hc</artifactId>
    <version>4.3.1</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.3.1</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient-cache</artifactId>
    <version>4.3.1</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore</artifactId>
    <version>4.3</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.3.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

在这之中:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore</artifactId>
    <version>4.3</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

在您的依赖项中,您有httpcore版本4.4.5,因此显然存在依赖项冲突,正如雅各布在评论和中所指出的那样链接的

通过Maven 依赖中介机制,您的构建将获取最新版本4.4.5,因为它是在您的依赖项中显式声明的,因此在运行时,java-bitpay-client类路径中将具有其依赖项之一的不同版本,这可能会导致最终的异常。

一个可能的解决方案是httpcore从您的依赖项中删除依赖项dependencies,并让它通过传递依赖项进入类路径(版本4.3然后版本应该进入)。

您还可以通过从项目的控制台运行来确认上述描述:

mvn dependency:tree -Dincludes=com.github.bitpay
Run Code Online (Sandbox Code Playgroud)

除了其他传递依赖项之外,您还应该获得httpcore.


旁注:我看到您定义了一个type具有 value 的依赖项jar。您可以省略,jaris the default value for a dependency type,即依赖项默认为 jar 文件。来自官方pom 参考

type:对应于依赖工件的packaging类型。这默认为jar.