簡體   English   中英

java.lang.NoSuchFieldError:bitpay SDK中的INSTANCE

[英]java.lang.NoSuchFieldError: INSTANCE in bitpay SDK

我想實現這個代碼

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");
    }
}

我包含了這些依賴項:

<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>

但是當我運行代碼時,我得到:

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)

問題 :您能否就我如何解決這個問題提出一些建議?

github上查看庫項目的pom.xml文件的maven依賴項,雖然不是相同的工件版本,但您可以看到java-bitpay-client依賴於org.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>

在這之中:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore</artifactId>
    <version>4.3</version>
</dependency>

在您的依賴項中,您改為使用httpcore版本4.4.5 ,因此顯然存在依賴沖突 ,正如雅各布在評論和鏈接的類似問題中所指出的那樣。

通過Maven依賴中介機制,您的構建將獲取最新的4.4.5版,因為它在您的依賴項中明確聲明,因此在運行時java-bitpay-client將在類路徑中具有其依賴項之一的不同版本,這可能會導致最終的例外。

一個可能的解決方案是從dependencies刪除httpcore依賴項,並讓它通過傳遞依賴項進入類路徑(然后版本4.3應該進入)。

您還可以通過從項目的控制台運行來確認上述說明:

mvn dependency:tree -Dincludes=com.github.bitpay

除了其他傳遞依賴之外,您還應該獲得httpcore


旁注:我看到你定義了一個具有值jar type的依賴項。 你可以省略, jar是依賴type的默認值,也就是說,依賴是默認的jar文件。 從官方pom參考

type :對應於依賴工件的packaging類型。 這默認為jar

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM