11

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?

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808
  • 1
    Possible duplicate of [this](http://stackoverflow.com/questions/21622885/java-lang-nosuchfielderror-instance?rq=1). – Jacob Sep 09 '16 at 19:19

1 Answers1

1

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.

Community
  • 1
  • 1
A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
  • I changed the version to 4.3. Now I get java.lang.NoClassDefFoundError: org/apache/http/ssl/SSLContext – user1285928 Sep 10 '16 at 22:25
  • @user1285928 you most probably missed an ending `s`, it's `SSLContexts` and [apparently](http://stackoverflow.com/questions/36914117/httpclient-jar-conflicts-how-to-fix-it) it's again because of version issue, the class moved package from version `4.3` to `4.4`, hence something relies on the latter version while the client library is using the former. – A_Di-Matteo Sep 10 '16 at 22:36
  • Another approach would then be to change the version of the whole `httpcomponents` family by redeclare them in your `dependencies` section with the latest version, as such you would consistently use the new version across the family. That is, take the dependencies reported here above and change their version. – A_Di-Matteo Sep 11 '16 at 06:52
  • I tested it but I get again java.lang.NoSuchFieldError: INSTANCE – Peter Penzov Sep 11 '16 at 07:46
  • You mean by applying the second approach? OK, then we're sure it requires the previous version, which makes sense. We then need to fix the no class found exception. – A_Di-Matteo Sep 11 '16 at 08:13
  • @PeterPenzov can you please add to your question the output of the command `mvn dependency:tree -Dincludes=org.apache.httpcomponents` executed on your project? – A_Di-Matteo Sep 11 '16 at 12:53
  • apparently you also had a further dependency initially, `httpclient`, which you didn't mention. Hence, at the first attemp, when removing `httpcore` from your dependencies (which fixed the INSTANCE error), you still then had `httpclient` version `4.5.2` among your direct dependencies > which would cause the NoClassDefFound error, makes sense, incompatibility. However, when you explicitely declared all, you still got an error, hence some other dependencies is playing a role in this game. Can you run again `mvn dependency:tree` (without the `-Dincludes`) in both cases and post it? – A_Di-Matteo Sep 11 '16 at 21:34