java.lang.ClassNotFoundException: org.apache.commons.codec.binary.Base64
Asked Answered
C

3

15

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;
    }
Conservatism answered 14/6, 2011 at 10:24 Comment(4)
make sure that Base64 is in your classpathWedekind
I hope this is just a mistake: String Key = z/0qfiE+ScjxHy2gSwmHqP0rZ6fT9zhVgsNt;Aromaticity
@oliholz: I included that jar file in my build pathConservatism
Are you aware that you posted non-compileable code? String values should go in quotes.Distich
W
40

just add commons-codec.jar into your classpath

Whodunit answered 14/6, 2011 at 10:27 Comment(3)
I included that jar file in my build path but again same errorConservatism
not on build path, you need to make available at run time. add it to WEB-INF/libWhodunit
@Jigar Joshi - Thank you very much for WEB-INF/lib comment.Hubbs
W
0

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

enter image description here

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>
Welcher answered 15/10, 2019 at 16:33 Comment(0)
T
-2

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

enter image description here

Transportation answered 27/10, 2020 at 9:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.