Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NoSuchFieldError: INSTANCE in bitpay SDK

I want to implement this code

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

I included these dependencies:

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

But when I run the code I get:

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)

Question: Can you give some advice how I can fix this?

like image 243
Peter Penzov Avatar asked Sep 07 '16 14:09

Peter Penzov


1 Answers

Looking at the maven dependencies of the pom.xml file of the library project on github, although not the same artifact version, you can see that the java-bitpay-client depends on several libraries from 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>

Among which:

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

In your dependencies you instead have httpcore version 4.4.5, hence there is clearly a dependency conflict, as also pointed out by Jacob in the comments and the linked similar question.

Via Maven dependency mediation mechanism, your build will pick up the latest, version 4.4.5, because it is explicitely declared among your dependencies, hence at runtime java-bitpay-client will have in the classpath a different version of one of its dependencies, which may cause the final exception.

A possible solution would then be to remove the httpcore dependency from your dependencies and let it come into the classpath via transitive dependencies (version 4.3 should then get into).

You can also confirm the description above by running from the console on your project:

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

You should get, among other transitive dependencies, also httpcore.


Side note: I see you defined a dependency with type having value jar. You can omit that, jar is the default value for a dependency type, that is, dependencies are by default jar files. From official pom reference:

type: Corresponds to the dependant artifact's packaging type. This defaults to jar.

like image 115
A_Di-Matteo Avatar answered Oct 14 '22 13:10

A_Di-Matteo