You need to change the import of your class:

import org.apache.commons.codec.binary.Base64;

And then change your class to use the Base64 class.

Here's some example code:

byte[] encodedBytes = Base64.encodeBase64("Test".getBytes());
System.out.println("encodedBytes " + new String(encodedBytes));
byte[] decodedBytes = Base64.decodeBase64(encodedBytes);
System.out.println("decodedBytes " + new String(decodedBytes));

Then read why you shouldn't use sun.* packages.


Update (2016-12-16)

You can now use java.util.Base64 with Java 8. First, import it as you normally do:

import java.util.Base64;

Then use the Base64 static methods as follows:

byte[] encodedBytes = Base64.getEncoder().encode("Test".getBytes());
System.out.println("encodedBytes " + new String(encodedBytes));
byte[] decodedBytes = Base64.getDecoder().decode(encodedBytes);
System.out.println("decodedBytes " + new String(decodedBytes));

If you directly want to encode string and get the result as encoded string, you can use this:

String encodeBytes = Base64.getEncoder().encodeToString((userName + ":" + password).getBytes());

See Java documentation for Base64 for more.

Answer from Frank on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Base64.Encoder.html
Base64.Encoder (Java Platform SE 8 )
July 21, 2026 - Encodes all bytes from the specified byte array into a newly-allocated byte array using the Base64 encoding scheme.
🌐
Baeldung
baeldung.com › home › java › java base64 encoding and decoding
Java Base64 Encoding and Decoding | Baeldung
February 18, 2026 - Java supports Base64 encoding and decoding features through the java.util.Base64 utility class.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Base64.html
Base64 (Java Platform SE 8 )
July 21, 2026 - Uses the "The Base64 Alphabet" as specified in Table 1 of RFC 2045 for encoding and decoding operation. The encoded output must be represented in lines of no more than 76 characters each and uses a carriage return '\r' followed immediately by a linefeed '\n' as the line separator.
🌐
Medium
medium.com › @AlexanderObregon › javas-base64-getencoder-method-explained-d3c331139837
Java’s Base64.getEncoder() Method Explained | Medium
October 22, 2024 - Let’s break down the core idea behind the Base64.getEncoder() method: Binary-to-Text Encoding: It converts any arbitrary binary data (represented as a byte array in Java) into a Base64-encoded string.
🌐
Base64Encode.org
base64encode.org › enc › java
Base64 Encoding of "java" - Online
Encode java to Base64 format with various advanced options. Our site has an easy to use online tool to convert your data.
🌐
GeeksforGeeks
geeksforgeeks.org › java › basic-type-base64-encoding-and-decoding-in-java
Basic Type Base64 Encoding and Decoding in Java - GeeksforGeeks
July 11, 2025 - The Basic encoding means no line feeds are added to the output and the output is mapped to a set of characters in A-Za-z0-9+/ character set and the decoder rejects any character outside of this set.
🌐
Medium
medium.com › @rkgupta1298 › base64-encoding-in-java-7ff71ea0cc65
Base64 Encoding and Decoding in JAVA | by coffeebytes | Medium
March 15, 2022 - Base64 encoding and decoding capabilities were added in java8 via the java.util.Base64 utility class.
Find elsewhere
🌐
Tutorialspoint
tutorialspoint.com › java › java_base64.htm
Java - Base64 Encoding and Decoding
This encoder uses characters lying in A-Za-z0-9+/ character set. Following is the snippet explaining the use of Base64 encoder. String stringToEncode = "TutorialsPoint?java8"; // Encode using basic encoder String base64encodedString = Base64.getEncoder().encodeToString(stringToEncode.getBytes("utf-8"));
🌐
HowToDoInJava
howtodoinjava.com › home › java 8 › java base64 encode and decode example
Java Base64 Encode and Decode Example
April 19, 2024 - Java 8 has added java.util.Base64 for Base-64 encoding and decoding purposes.
🌐
Base64.Guru
base64.guru › home › developers › java
Java Base64 Encode | Java | Developers | Base64
Since Java 1.8 developers can use the Base64.Encoder class to encode data to Base64. This class provides several methods for encoding byte data to Base64 using algorithms specified in RFC 4648 and RFC 2045.
🌐
Javatpoint
javatpoint.com › java-base64-encode-decode
Java Base64 Encode Decode - javatpoint
Java Base64 Encode Decode examples and topics on functional interface, anonymous class, lambda for list, lambda for comparable, default methods, method reference, java date and time, java nashorn, java optional, stream, filter etc.
🌐
How to do in Java
howtodoinjava.com › home › java security › java base64 encode and decode a file (with examples)
Java Base64 Encode and Decode a File (with Examples)
April 19, 2024 - Use the Base64.Encoder to encode the byte array into another byte array or String, as per requirements.
🌐
Stack Abuse
stackabuse.com › encoding-and-decoding-base64-strings-in-java
Encoding and Decoding Base64 Strings in Java
September 7, 2023 - We manually encoded a string to get a better understanding of the process, preceding many code examples. Using the Base64 class in Java allows us to make different types of encoders and decoders - optimized for encoding and decoding basic strings, URLs and MIME types.
🌐
Java67
java67.com › 2016 › 09 › Java-base64-encoding-and-decoding-example-in-JDK8.html
Base64 Encoding and Decoding Examples in Java 8 and Before | Java67
You can use Base64.Encoder to encode a byte array or String and Base64.Decoder to decode a base64 encoded byte array or String in Java 8. The JDK 8 API also provides different types of Base64 encoders e.g.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Java 8 Base64 Encoding and Decoding (With Examples) - Java Code Geeks
September 8, 2020 - In java 8, We can use mainly 3 types of encoding and decoding capabilities as standard. All of the classes are related to this are in java.util.base64 package.
🌐
GeeksforGeeks
geeksforgeeks.org › java › base64-java-encode-and-decode-a-string
Base64 Java Encode and Decode a String - GeeksforGeeks
July 23, 2025 - Base64 encoding allows encoding binary data as text strings for safe transport, at the cost of larger data size. It is commonly used when there is a need to encode binary data that needs to be stored or transferred via media that are designed ...
🌐
Educative
educative.io › answers › how-to-perform-base64-encoding-and-decoding-in-java
How to perform base64 encoding and decoding in Java
Get encoder using Base64.getEncoder(). Generate an encoded string by passing the string as bytes to the encodeToString method of the encoder object.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › technotes › guides › lang › Base64Sample.java
/ Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
//encoding byte array into base 64 byte[] encoded = urec.encode(myurl.toString().getBytes("UTF8")); byte[] strenc =enc.encode(str.getBytes("UTF-8")); System.out.println("Base64 Encoded URL : " + new String(encoded,"UTF-8")); System.out.println("Base64 Encoded String : " + new String(strenc,"UTF-8")); //decoding byte array into base64 Base64.Decoder urdc= Base64.getUrlDecoder(); Base64.Decoder dec= Base64.getDecoder(); byte[] urdec=urdc.decode(encoded); byte[] strdec=dec.decode(strenc); System.out.println("Base64 Decoded URL : " + new String(urdec,"UTF-8")); System.out.println("Base64 Decoded String : " + new String(strdec,"UTF-8")); } catch(Exception e){ System.out.println("Invalid URL Exception"); } }