Skip to main content

How to Base64url-Encode Bytes in Java with jjwt Encoders.BASE64URL

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

jjwt, the Java JWT library, comes with its own base64 encoders in io.jsonwebtoken.io.Encoders. Encoders.BASE64URL.encode(bytes) writes the URL-safe, unpadded base64url that JWTs use, and you can call it in your own code too.

Syntax​

Syntax
String <encoded> = Encoders.BASE64URL.encode(<bytes>);
String <encoded> = Encoders.BASE64.encode(<bytes>);
PartWhat it does
Encoders.BASE64URLBase64url: - and _ instead of + and /, with no = padding.
Encoders.BASE64Standard base64, with padding.
.encode(<bytes>)Takes a byte[] and returns the encoded String.

The jjwt API documentation for Encoders: BASE64URL is a "Very fast Base64Url encoder guaranteed to work in all >= Java 7 JDK and Android environments."

The Example​

The example turns the text Hi?> into UTF-8 bytes and encodes them with both encoders. Those four characters were chosen because their base64 contains a / and padding.

Prerequisites​

  • A JDK and Maven. This guide uses OpenJDK 25.0.5.1 and Maven 3.10.2.

Step 1: Create the Project​

Create a folder named demo with this pom.xml:

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 is the part of jjwt your code calls, and it holds the encoders. exec-maven-plugin lets Maven run the program.

Step 2: Write the Code​

Create src/main/java/com/example/Main.java inside demo:

Main.java, which turns Hi?> into UTF-8 bytes and prints Encoders.BASE64.encode and Encoders.BASE64URL.encode of them.

src/main/java/com/example/Main.java
package com.example;

import io.jsonwebtoken.io.Encoders;

import java.nio.charset.StandardCharsets;

public class Main {
public static void main(String[] args) {
byte[] data = "Hi?>".getBytes(StandardCharsets.UTF_8);

System.out.println(Encoders.BASE64.encode(data));
System.out.println(Encoders.BASE64URL.encode(data));
}
}

Step 3: Run It​

Terminal
cd demo
mvn -q compile exec:java
Output
SGk/Pg==
SGk_Pg

A zsh terminal where mvn compile exec prints SGk/Pg== and SGk_Pg.

-q keeps Maven quiet, compile builds the code, and exec:java runs the class named in pom.xml.

What Happened​

  • Encoders.BASE64 gave SGk/Pg==: a / and two = of padding.
  • Encoders.BASE64URL gave SGk_Pg: the / became _, and the padding is gone, which is the form every piece of a JWT uses.

Common Mistakes​

MistakeWhat happensDo this instead
Using Encoders.BASE64 for a JWT pieceThe text can contain +, / and =, and jjwt's base64url decoder rejects themUse Encoders.BASE64URL
Calling getBytes() with no charset before encodingThe bytes depend on the platform's default charsetUse getBytes(StandardCharsets.UTF_8)
Adding jjwt-impl only for the encodersNot needed; they're in jjwt-apiDepend on jjwt-api

Troubleshooting​

SymptomLikely causeFix
package io.jsonwebtoken.io does not existjjwt-api isn't a dependencyAdd it to pom.xml
The result ends in =The code used Encoders.BASE64Use Encoders.BASE64URL

FAQ​

Is this different from java.util.Base64?​

Not in the result: Base64.getUrlEncoder().withoutPadding().encodeToString(bytes) gives the same text. jjwt's encoder saves you choosing the padding setting.

How do I decode it again?​

With Decoders.BASE64URL.decode(text), from the same package.

Conclusion​

Encoders.BASE64URL.encode(bytes) turns bytes into unpadded base64url, the same encoding jjwt uses inside every token. It comes with jjwt-api, so code that already uses jjwt needs nothing more.

Additional Resources​