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 using the Base64 encoding scheme, writing the resulting bytes to the given output byte array, starting at offset 0.
🌐
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 - Learn how Java's Base64.getEncoder() method encodes binary data into a Base64 string for safe transmission, including usage in JWTs and secure web apps.
🌐
GeeksforGeeks
geeksforgeeks.org › java › basic-type-base64-encoding-and-decoding-in-java
Basic Type Base64 Encoding and Decoding in Java - GeeksforGeeks
July 11, 2025 - // Java program to demonstrate ... System.out.println("Sample String:\n" + sample); // Encode into Base64 format String BasicBase64format = Base64.getEncoder() .encodeToString(sample.getBytes()); // print 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.
🌐
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
🌐
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.
🌐
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"));
🌐
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.
🌐
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 ...
🌐
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 - Since Java 8, the java.util.Base64 class provides convenient methods for encoding and decoding files to and from Base64 format.
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › util › Base64.Encoder.html
Base64.Encoder (Java SE 21 & JDK 21)
January 20, 2026 - Encodes all bytes from the specified byte array using the Base64 encoding scheme, writing the resulting bytes to the given output byte array, starting at offset 0.
🌐
InfoWorld
infoworld.com › home › software development › programming languages › java
Base64 encoding and decoding in Java 8 | InfoWorld
December 6, 2017 - Also, this variant is useful when the encoded data is to be used for a filename because Unix and Windows filenames cannot contain /. Java 8 introduced a Base64 API consisting of the java.util.Base64 class along with its Encoder and Decoder nested static classes.
🌐
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.
🌐
GitHub
github.com › openjdk › jdk › blob › master › src › java.base › share › classes › java › util › Base64.java
jdk/src/java.base/share/classes/java/util/Base64.java at master · openjdk/jdk
import jdk.internal.access.JavaLangAccess; import jdk.internal.access.SharedSecrets; import jdk.internal.util.Preconditions; import jdk.internal.vm.annotation.IntrinsicCandidate; · /** * This class consists exclusively of static methods for obtaining · * encoders and decoders for the Base64 encoding scheme.
Author: openjdk
🌐
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.