java.lang.ClassNotFoundException: org.apache.commons.codec.binary.Base64

2022-09-03 09:25:46

在这里,我将三个参数传递给此符号方法。在此行中

signature = new String(Base64.encodeBase64(mac.doFinal(data.getBytes(UTF_8_Encoding))));

我收到错误:

SEVERE: Servlet.service() for servlet [com.asp.amz.amzServlet] in context with path [/amazon] 抛出异常 [Servlet execution threw an exception] with root cause java.lang.ClassNotFoundException: org.apache.commons.codec.binary.Base64

String Key = "z/0qfiE+ScjxHy2gSwmHqP0rZ6fT9zhVgsNt";
String signatureMethod = "HmacSHA256";
String data = "sandbox.amazon.com/cobranded-ui/actions/start?callerKey=AKIAJZOKEUCXF7RKSCNA&callerReference=callerReferenceSingleUse&currencyCode=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;
    }

答案 1

只需将commons-codec.jar添加到您的类路径中


答案 2

可能有两个原因:

第一个原因:1.罐子本身没有添加到pom.xml中。在这种情况下,请在 pom 中添加依赖项版本.xml该版本具有您收到错误的类

第二个原因:当项目中添加了多个 jar 并且靠近项目的 jar 版本没有该类时。您可以在以下命令检查项目中最近的jar

mvn dependency:tree -Dverbose -Dincludes=jar-name-casuing-conflict

enter image description here

一旦你确定了具有该类的Jar版本。您可以通过直接在项目 pom 中添加该 jar 来解决.xml

或者,您可以排除更靠近您的项目的 jar,该 jar 没有该类,并且可以指向项目中的另一个 jar。

示例:它将排除项目“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>

推荐