我想实现 this代码
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上library项目的pom.xml
文件的maven依赖,虽然不是同一个artifact版本,但是可以看到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
,因此显然存在 依赖项冲突,正如 Jacob 所指出的在评论和 linked 类似的问题中。
通过 Maven dependency mediation 机制,您的构建将选择最新版本 4.4.5
,因为它在您的依赖项中明确声明,因此在运行时 java-bitpay-client
将在类路径中具有其依赖项之一的不同版本,这可能会导致最终异常。
一个可能的解决方案是从你的 dependencies
中删除 httpcore
依赖,并让它通过传递依赖进入类路径(版本 4.3
然后应该进入)。
您还可以通过从项目的控制台运行来确认上面的描述:
mvn dependency:tree -Dincludes=com.github.bitpay
在其他传递依赖项中,您还应该获得 httpcore
。
旁注:我看到您使用 type
定义了一个依赖项,其值为 jar
。可以省略,jar
是依赖type
的默认值,即依赖默认是jar文件。来自官方 pom reference :
type
: Corresponds to the dependant artifact'spackaging
type. This defaults tojar
.
关于java.lang.NoSuchFieldError : INSTANCE in bitpay SDK 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39372665/