How to Decode Base64url in Java with jjwt Decoders.BASE64URL
Decoders.BASE64URL.decode(text) from jjwt turns base64url text, such as one piece of a JWT, back into bytes. It's strict: plain base64 with / or = makes it throw a DecodingException instead of guessing.
Syntax
byte[] <bytes> = Decoders.BASE64URL.decode(<text>);
String <decoded> = new String(<bytes>, StandardCharsets.UTF_8);
| Part | What it does |
|---|---|
Decoders.BASE64URL.decode(<text>) | Decodes base64url text, with or without padding, into a byte[]. |
new String(<bytes>, StandardCharsets.UTF_8) | Turns the bytes into text, when they are text. |
DecodingException | Thrown for a character that base64url doesn't use. |
The jjwt API documentation for Decoders: BASE64URL is a "Very fast Base64Url decoder guaranteed to work in all >= Java 7 JDK and Android environments."
The Example
The example decodes SGk_Pg, the base64url form of Hi?>, and prints it as text. Then it tries SGk/Pg==, the plain base64 form of the same text, and prints the error.
Prerequisites
Step 1: Create the Project
Create a folder named demo with this pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.release>25</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.13.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<mainClass>com.example.Main</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
jjwt-api holds the decoders, and exec-maven-plugin lets Maven run the program.
Step 2: Write the Code
Create src/main/java/com/example/Main.java inside demo:

package com.example;
import io.jsonwebtoken.io.DecodingException;
import io.jsonwebtoken.io.Decoders;
import java.nio.charset.StandardCharsets;
public class Main {
public static void main(String[] args) {
byte[] data = Decoders.BASE64URL.decode("SGk_Pg");
System.out.println(new String(data, StandardCharsets.UTF_8));
try {
Decoders.BASE64URL.decode("SGk/Pg==");
} catch (DecodingException e) {
System.out.println(e.getMessage());
}
}
}
Step 3: Run It
cd demo
mvn -q compile exec:java
Hi?>
Illegal base64url character: '/'

-q keeps Maven quiet, compile builds the code, and exec:java runs the class named in pom.xml.
What Happened
SGk_Pgdecoded to the bytes ofHi?>without any padding.SGk/Pg==stopped at the/, which base64url doesn't use, and the decoder threwDecodingException: Illegal base64url character: '/'.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Decoding plain base64 with BASE64URL | DecodingException: Illegal base64url character: '/' | Use Decoders.BASE64 for plain base64 |
Decoding base64url with Decoders.BASE64 | DecodingException: Illegal base64 character: '_' | Use Decoders.BASE64URL |
Turning binary data, such as a signature, into a String | The bytes aren't text and come out garbled | Keep them as byte[] |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Illegal base64url character: '*' or another character | The input isn't base64url, or has a stray character | Check where the text came from |
cannot find symbol: class DecodingException | The import is missing | Add import io.jsonwebtoken.io.DecodingException; |
FAQ
Can I decode a JWT's payload with this?
Yes: split the token at its dots and decode the middle piece. That only reads the claims; it doesn't verify them, so use it for reading, and verify the token with jjwt's parser before trusting anything in it.
Does it need the padding?
No. SGk_Pg decoded without the = that plain base64 would add.
Conclusion
Decoders.BASE64URL.decode(text) turns base64url into bytes and refuses anything else, and new String(bytes, StandardCharsets.UTF_8) turns text bytes back into a string.
Additional Resources
- Decoders, in the jjwt API documentation.
- RFC 4648, section 5: base64 with the URL and filename safe alphabet.
