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
🌐
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.
🌐
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.
🌐
Java Guides
javaguides.net › 2020 › 02 › base64-encode-online.html
Base64 Encode Online
August 20, 2023 - This Base64 Encode Online tool processes the data entirely client-side, meaning your data never leaves your browser or gets transmitted over the internet.
🌐
OneCompiler
onecompiler.com › java › 3y2k3s2hz
Base64 - Java - OneCompiler
import java.util.*; public class ... trash"; String BasicBase64format = Base64.getEncoder() .encodeToString(sample.getBytes()); System.out.println("Encoded String:\n" + BasicBase64format); } } ... Write, Run & Share Java code online using OneCompiler's Java online compiler ...
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › java › base64-java-encode-and-decode-a-string
Base64 Java Encode and Decode a String - GeeksforGeeks
July 23, 2025 - It is commonly used when there ... JSON document, etc.). In Java, the java.util.Base64 class provides static methods to encode and decode between binary and base64 formats....
🌐
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.
Find elsewhere
🌐
Coding
coding.tools › base64-encode
Base64 Encode Online Tool - Coding.Tools
import java.util.Base64; public String encode(String str) { byte[] encodedBytes = Base64.getEncode().encode(str.getBytes()); String text = new String(encodedBytes); return text; }
🌐
Base64.Guru
base64.guru › home › developers › java
Java Base64 Encode | Java | Developers | Base64
import java.util.Base64; class Base64Encode { public static void main(String[] args) { String str = "guru"; String b64 = Base64.getEncoder().encodeToString(str.getBytes()); System.out.println(b64); //-> "Z3VydQ==" } }
🌐
Base64.Guru
base64.guru › home › developers › java
Java Base64 Decode | Java | Developers | Base64
Since Java 1.8 developers can use the Base64.Decoder class to decode Base64 values. This class provides several methods for decoding Base64 to byte data using algorithms specified in RFC 4648 and RFC 2045. In addition, Java provides the Base64.Encoder class for encoding data to Base64.
🌐
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.
🌐
Blogger
javarevisited.blogspot.com › 2016 › 10 › base64-encoding-example-in-java-8.html
How to Base 64 Encoding Decoding in Java 8 - Example Tutorial
April 24, 2023 - You can use the static getEncoder() and getDecoder() method of Base64 class to obtain the basic base64 encoder and decoder and use their encode() and decode() for base64 encoding and decoding.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Java 8 Base64 Encoding and Decoding (With Examples) - Java Code Geeks
September 8, 2020 - Learn different ways to do base64 encoding and decoding functionality in java and using java 8 api, and also examples using the apache commons-codec. 1.
🌐
GitHub
gist.github.com › EmilHernvall › 953733 › 6bd1ee76db79c5e9e090bbd0fd40a5033270a30a
Simple base64-encoder for java · GitHub
//Java String input = "testing0"; System.out.println(String.format("str: '%s'", input)); System.out.println(String.format("enc: '%s'", encode(input.getBytes()))); //output str: 'testing0' enc: 'dGVzdGluZz=' //PHP $input = 'dGVzdGluZz='; echo(sprintf("enc: '%s'", $input)); echo(sprintf("dec: '%s'", base64_decode($input))); //output enc: 'dGVzdGluZz=' dec: 'testing'
🌐
Tutorialspoint
tutorialspoint.com › java › java_base64.htm
Java - Base64 Encoding and Decoding
Encoded String: VHV0b3JpYWxzUG9pbnQ/amF2YQ== Decoded String: TutorialsPoint?java · URL Base64 encoder encodes the provided URL and makes it URL and filename safe. This encoder uses characters lying in A-Za-z0-9+/ character set.
🌐
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.
🌐
Educative
educative.io › answers › how-to-perform-base64-encoding-and-decoding-in-java
How to perform base64 encoding and decoding in Java
To use the URL encoder and decoder, use getUrlEncoder and the getUrlDecooder method in the Base64 class, and call encodeToString and the decode method respectively.
🌐
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.