简体   繁体   中英

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

Here I'm passing three parameters to this sign method. In this line

signature = new String(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

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;
    }

just add commons-codec.jar into your classpath

Get commons-codec.jar from Apache HTTP Components

There could be Two Reason for that:

First Reason: 1. The jar itself is not added in the pom.xml. In that case, add the dependency version in the pom.xml which has the class for which you are getting error

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. You can check the nearest jar in your project below command

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

在此处输入图像描述

Once you identify the Jar version which has that class. You can resolve by adding that jar directly in your project pom.xml

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.

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

   <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>

when you have error you can get all jar in picture,then your project will be ok.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM