[英]java.lang.ClassNotFoundException: org.apache.commons.codec.binary.Base64
Here I'm passing three parameters to this sign method.在这里,我将三个参数传递给这个 sign 方法。 In this line在这一行
signature = new String(Base64.encodeBase64(mac.doFinal(data.getBytes(UTF_8_Encoding))));签名=新字符串(Base64.encodeBase64(mac.doFinal(data.getBytes(UTF_8_Encoding))));
I'm getting error:我收到错误:
SEVERE: Servlet.service() for servlet [com.asp.amz.amzServlet] in context with path [/amazon] threw exception [Servlet execution threw an exception] with root cause java.lang.ClassNotFoundException: org.apache.commons.codec.binary.Base64严重:servlet [com.asp.amz.amzServlet] 在路径 [/amazon] 上下文中的 Servlet.service() 引发异常 [Servlet 执行引发异常],根本原因 java.lang.ClassNotFoundException:org.apache.com codec.binary.Base64
String Key = "z/0qfiE+ScjxHy2gSwmHqP0rZ6fT9zhVgsNt";
String signatureMethod = "HmacSHA256";
String data = "sandbox.amazon.com/cobranded-ui/actions/start?callerKey=AKIAJZOKEUCXF7RKSCNA&callerReference=callerReferenceSingleUse¤cyCode=USD&paymentReason=HarryPotter%201-5%20DVD%20set&pipelineName=SingleUse&returnURL=http%3A%2F%2Flocalhost%3A8888%2Famazon&signatureMethod=HmacSHA256&signatureVersion=2&transactionAmount=5&version=2009-01-09";
private static String sign(String data, String key, String signatureMethod) throws SignatureException
{
System.out.println(" In sign block ");
String signature = "";
try {
System.out.println(" In sign Try block ");
Mac mac = Mac.getInstance(signatureMethod);
mac.init(new SecretKeySpec(key.getBytes(), signatureMethod));
signature = new String(Base64.encodeBase64(mac.doFinal(data.getBytes(UTF_8_Encoding))));
System.out.println(" In sign Try block ");
} catch (Exception e) {
System.out.println(" In sign catch block ");
throw new SignatureException("Failed to generate signature: " + e.getMessage(), e);
}
System.out.println(" End sign block " + signature);
return signature;
}
just add commons-codec.jar into your classpath只需将commons-codec.jar添加到您的类路径中
Get commons-codec.jar
from Apache HTTP Components从Apache HTTP 组件获取commons-codec.jar
There could be Two Reason for that:可能有两个原因:
First Reason: 1. The jar itself is not added in the pom.xml.第一个原因: 1. jar本身没有添加到pom.xml中。 In that case, add the dependency version in the pom.xml which has the class for which you are getting error在这种情况下,在 pom.xml 中添加依赖版本,其中包含您遇到错误的 class
Second Reason: When there are multiple jars added in the project and the jar version which is near to the project doesn't have that class.第二个原因:当项目中添加了多个jars,而项目附近的jar版本没有那个class。 You can check the nearest jar in your project below command您可以在下面的项目中检查最近的 jar 命令
mvn dependency:tree -Dverbose -Dincludes=jar-name-casuing-conflict
Once you identify the Jar version which has that class.一旦您确定了具有 class 的 Jar 版本。 You can resolve by adding that jar directly in your project pom.xml您可以通过在项目 pom.xml 中直接添加 jar 来解决
or You can exclude the jar that is nearer to your project which doesn't has that class and can point to another jar in the project.或者您可以排除离您的项目更近的 jar 没有该 class 并且可以指向项目中的另一个 jar。
Example: it will exclude the jar in the project "org.cassandraunit" then it will see the immediate next jar that's close to the project will be used in your project示例:它将排除项目“org.cassandraunit”中的 jar 然后它会立即看到下一个靠近项目的 jar 将在您的项目中使用
<dependency>
<groupId>org.cassandraunit</groupId>
<artifactId>cassandra-unit</artifactId>
<version>3.1.3.2</version>
<exclusions>
<exclusion>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</exclusion>
</exclusions>
</dependency>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.