我想实现这代码
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)问题:你能给我一些建议来解决这个问题吗?
发布于 2016-09-10 21:37:46
查看库项目的pom.xml文件在github上的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将在类路径中包含一个依赖项的不同版本,这可能会导致最终的异常。
然后,一个可能的解决方案是从您的httpcore中删除dependencies依赖项,并让它通过传递依赖项进入类路径( 4.3版本应该进入)。
您还可以通过在项目的控制台上运行来确认上述描述:
mvn dependency:tree -Dincludes=com.github.bitpay在其他传递依赖项中,您还应该获得httpcore。
附带注意:我看到您使用type定义了一个具有值jar的依赖项。您可以省略,jar是依赖项type的默认值,也就是说,依赖项是默认的jar文件。来自官方pom参考
type:对应于依赖的工件的packaging类型。这默认为jar。
https://stackoverflow.com/questions/39372665
复制相似问题