Skip to main content

Fixed: "Unable to load class named [io.jsonwebtoken.impl.security.KeysBridge]" in JJWT

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

A Java program that creates a JWT with JJWT compiled without a problem and then stopped with Unable to load class named [io.jsonwebtoken.impl.security.KeysBridge]. Its pom.xml had only jjwt-api, the part of JJWT that code calls. The part that does the work, jjwt-impl, has to be there when the program runs.

What I Was Doing​

I was writing small Java examples for JWT how-to posts. An earlier example of mine only encoded text with JJWT's Encoders, which needs nothing but the jjwt-api library, so its pom.xml listed only that one. I copied that pom.xml for a new example that creates a key and signs a token. The code compiled, and then the program stopped with the error below.

The example below shows the same thing with nothing else in the way: a program that signs a token with one claim.

What I Expected​

The program to print a signed token.

What Happened Instead​

Create a folder named demo, with this pom.xml in it:

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>
<jjwt.version>0.13.0</jjwt.version>
</properties>

<dependencies>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>${jjwt.version}</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>

The only dependency is jjwt-api. The exec-maven-plugin entry names com.example.Main as the class to run.

Then the program, in src/main/java/com/example/Main.java:

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

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;
import javax.crypto.SecretKey;

public class Main {

public static void main(String[] args) {
// The signing key: 32 random bytes, written as base64.
// It's in the code only for this example; a real app loads it from its configuration.
byte[] secret = Decoders.BASE64.decode("C4iNOOORzmu+fnrPK29yq/9n92b/zf5mOs5KvOMaY64=");
SecretKey key = Keys.hmacShaKeyFor(secret);
String token = Jwts.builder().subject("ada").signWith(key).compact();
System.out.println(token);
}
}

It decodes a base64 secret into bytes, turns them into a key with Keys.hmacShaKeyFor, and signs a token whose only claim is sub.

In the demo folder, build and run it:

Terminal
mvn -q compile exec:java
Output
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.7.0:java (default-cli) on project demo: An exception occurred while executing the Java class. null: ExceptionInInitializerError: Unable to load class named [io.jsonwebtoken.impl.security.KeysBridge] from the thread context, current, or system/application ClassLoaders. All heuristics have been exhausted. Class could not be found. Have you remembered to include the jjwt-impl.jar in your runtime classpath? -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

A zsh terminal where mvn -q compile exec fails with an ExceptionInInitializerError: Unable to load class named io.jsonwebtoken.impl.security.KeysBridge, asking whether the jjwt-impl.jar was included in the runtime classpath.

-q keeps Maven quiet, compile builds the program and exec:java runs it. The command is the same in zsh, bash and PowerShell. The program compiled, so the code itself was fine. It failed while running, with an ExceptionInInitializerError, before it printed anything.

What I Checked​

  • The message. It names a class, io.jsonwebtoken.impl.security.KeysBridge, says that no class loader could find it, and ends with a question: "Have you remembered to include the jjwt-impl.jar in your runtime classpath?"
  • Where the classes live. Listing the two jars Maven downloaded, jjwt-api-0.13.0.jar holds Jwts and Keys, the classes the program calls, and jjwt-impl-0.13.0.jar holds KeysBridge. That's why the program compiled: the compiler only needs the classes the code names.
  • The JJWT README. Its installation section lists three Maven dependencies: jjwt-api, then jjwt-impl and jjwt-jackson, both with <scope>runtime</scope>. Understanding JJWT Dependencies explains: "Notice the above JJWT dependency declarations all have only one compile-time dependency and the rest are declared as runtime dependencies." It warns: "Never add the jjwt-impl .jar to your project with compile scope - always declare it with runtime scope."

So the error was right: the pom.xml I copied was made for a program that never signs anything, and it left out the two parts JJWT needs at run time.

The Fix​

Add jjwt-impl and jjwt-jackson to pom.xml, both with runtime scope, as the README lists them:

pom.xml with jjwt-api, plus jjwt-impl and jjwt-jackson with runtime scope, a jackson-bom 2.23.0 import in dependencyManagement, and the exec-maven-plugin entry that names com.example.Main.

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>
<jjwt.version>0.13.0</jjwt.version>
</properties>

<dependencyManagement>
<dependencies>
<!-- jjwt-jackson asks for Jackson 2.12.7.1. This makes Maven use the newest Jackson 2 instead. -->
<dependency>
<groupId>com.fasterxml.jackson</groupId>
<artifactId>jackson-bom</artifactId>
<version>2.23.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<dependencies>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>${jjwt.version}</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>${jjwt.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>${jjwt.version}</version>
<scope>runtime</scope>
</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-impl does the work behind Keys and Jwts, KeysBridge included.
  • jjwt-jackson turns the token's header and claims into JSON with Jackson. The README names jjwt-gson as the choice for Gson.
  • runtime scope, in the words of Maven's dependency scopes: "This scope indicates that the dependency is not required for compilation, but is for execution." Your code can't come to depend on JJWT's internal classes, because the compiler never sees them.
  • The jackson-bom entry in dependencyManagement is separate from the error. jjwt-jackson asks for Jackson 2.12.7.1, and this entry makes Maven use Jackson 2.23.0 instead, the newest Jackson 2 release.

The Result​

Run it again:

Terminal
mvn -q compile exec:java
Output
eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJhZGEifQ.atRrz4IWSoGBEf33EP2VKwwRNCz32yjpbjeoGLhOsdQ

A zsh terminal where mvn -q compile exec prints a signed JWT.

The program printed a token signed with HS256, whose payload is {"sub":"ada"}.

How to Avoid It​

  • Start every JJWT project with all three dependencies: jjwt-api, and jjwt-impl and jjwt-jackson with runtime scope.
  • Don't take a pom.xml from a project that used less of a library. Check its dependencies against the library's installation instructions.
  • Read the question at the end of the message. JJWT names the missing jar there.

Troubleshooting​

SymptomLikely causeFix
Unable to load class named [io.jsonwebtoken.impl.security.KeysBridge]jjwt-impl isn't in pom.xmlAdd it with runtime scope
The code compiles, but the program fails as soon as it calls JJWTjjwt-impl isn't there when the program runsAdd it with runtime scope

FAQ​

Why does JJWT keep jjwt-impl out of compile scope?​

So programs only use the parts that keep working across versions. The README: "JJWT guarantees semantic versioning compatibility for all of its artifacts except the jjwt-impl .jar."

Is the fix the same in Gradle?​

The README lists the same three parts for Gradle, with jjwt-impl and jjwt-jackson as runtimeOnly dependencies. This post only ran the Maven build.

Conclusion​

JJWT comes in three parts, and code compiles against only one of them, jjwt-api. When the program runs, jjwt-impl and jjwt-jackson must be there too, so list them in pom.xml with runtime scope.

Additional Resources​