1

I have the following snippet and I have some unresolved classes - mainly, Base64

String auth = username + ":" + password; 
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(StandardCharsets.ISO_8859_1));
String authHeader = "Basic " + new String(encodedAuth);
request.setHeader(HttpHeaders.AUTHORIZATION, authHeader);

I've tried to import the commons-codec maven repository but in Eclipse it still says cannot resolve class:

<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.12</version>
</dependency>

We're kinda stuck for now using JDK7 which may be the issue. Is this Base64 class only available from Java 8? Otherwise, how do I import it into my application?

4
  • The standard java.util.Base64 class is only available in java 8+. The reason you can't see the one from commons-codec is not your java version. Please check your project configuration Commented Jul 23, 2019 at 9:02
  • According to commons.apache.org/proper/commons-codec this version of commons-codec requires Java 1.7 so should be fine(?) Commented Jul 23, 2019 at 9:16
  • 1
    Your code seems to compile with commons-codec 1.12. Do you have the right import ? import org.apache.commons.codec.binary.Base64; Commented Jul 23, 2019 at 9:37
  • That's it, I manually wrote out the import statement and it resolved. Strangely the commons-codec doesn't show in the Eclipse popup. I'm just running against a test environment at the moment, so might be something I need to keep an eye out for whether that library is being imported or not. Thanks for posting the proper package anyway. Commented Jul 23, 2019 at 10:07

1 Answer 1

1

If you check the Javadoc, it clearly says that Base64 is Since 1.8.

https://docs.oracle.com/javase/8/docs/api/java/util/Base64.html

Not sure about the commons-codec one, you must have something wrong in Eclipse (not suprised considering how crap its Maven integration is).

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, but that indicates that Base64 is included with Java 1.8, right? So how do versions prior to 8 import Base64 codecs if they are not available in the JDK? Also Mavan seems fine, it's only this repo that I'm having issues with. According to commons.apache.org/proper/commons-codec it requires java 1.7, so should be OK(?)
Yes the commons-codec one should work. Not sure why it is not showing on your Eclipse IDE. On my IntelliJ it loads instantly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.