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?

Martyn
  • 6,031
  • 12
  • 55
  • 121
  • 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 – ernest_k Jul 23 '19 at 09:02
  • According to https://commons.apache.org/proper/commons-codec/ this version of commons-codec requires Java 1.7 so should be fine(?) – Martyn Jul 23 '19 at 09: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;` – Benoit Jul 23 '19 at 09: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. – Martyn Jul 23 '19 at 10:07

1 Answers1

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

jbx
  • 21,365
  • 18
  • 90
  • 144
  • 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 https://commons.apache.org/proper/commons-codec/ it requires java 1.7, so should be OK(?) – Martyn Jul 23 '19 at 09:15
  • 2
    Yes the commons-codec one should work. Not sure why it is not showing on your Eclipse IDE. On my IntelliJ it loads instantly. – jbx Jul 23 '19 at 09:31