Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Java: No suitable driver found for jdbc: URL

DriverManager found no driver accepting the URL. Put the driver on the runtime classpath, match the URL prefix and handle split classloaders.

~5 min readJava
#java#jdbc#classpath#maven
Bold white headline reading NO JDBC DRIVER with a Java driver crate holding an amber empty jar slot beside a cyan URL plate.

Introduction & Quick Summary

A service that connected fine from the IDE fails on the server at startup:

code
java.sql.SQLException: No suitable driver found for jdbc:mysql://db:3306/shop at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)

Quick answer: DriverManager walked every registered driver and none of them claimed that URL. The driver JAR is missing from the runtime classpath, or it is loaded by a classloader that the calling code cannot see.

xml
<!-- the driver must be a runtime dependency of the deployed artifact --> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> <version>9.1.0</version> <scope>runtime</scope> </dependency>

Since JDBC 4.0 the driver registers itself from META-INF/services/java.sql.Driver when its JAR is on the classpath, so Class.forName("com.mysql.cj.jdbc.Driver") is no longer required — and adding it does not help when the JAR is absent. The missing piece is almost always packaging, not code.

Three distinct failures are often reported under this one complaint:

  • No suitable driver found for jdbc:... — no registered Driver accepts the URL.
  • Failed to determine a suitable driver class — Spring Boot could not infer a driver from the URL, usually a missing dependency.
  • No driver suitable for jdbc:... in a container — the driver is inside a nested JAR the loader cannot scan.

Core Mechanics, Signatures & Reproducible Setup

DriverManager iterates registeredDrivers, calls driver.acceptsURL(url) on each, and instantiates the first match. The service loader populates that list the first time DriverManager is used, from the classloader that initialized it.

java
import java.sql.*; public class Probe { public static void main(String[] args) throws Exception { DriverManager.getDrivers().asIterator().forEachRemaining(d -> System.out.println(d.getClass().getName() + " v" + d.getMajorVersion())); try (Connection c = DriverManager.getConnection( "jdbc:mysql://db:3306/shop", "app", "secret")) { System.out.println("connected: " + c.getMetaData().getDatabaseProductName()); } } }

Run it with the deployed artifact, not with the IDE classpath:

bash
java -cp "app.jar:lib/*" Probe
DatabaseURL prefixDriver classMaven artifact
MySQLjdbc:mysql://com.mysql.cj.jdbc.Drivercom.mysql:mysql-connector-j
PostgreSQLjdbc:postgresql://org.postgresql.Driverorg.postgresql:postgresql
SQL Serverjdbc:sqlserver://com.microsoft.sqlserver.jdbc.SQLServerDrivercom.microsoft.sqlserver:mssql-jdbc
Oraclejdbc:oracle:thin:@oracle.jdbc.OracleDrivercom.oracle.database.jdbc:ojdbc11
H2jdbc:h2:org.h2.Drivercom.h2database:h2
MariaDBjdbc:mariadb://org.mariadb.jdbc.Driverorg.mariadb.jdbc:mariadb-java-client

Key Comparison Table

CauseSymptom detailConfirming checkFix
Driver JAR absentlist of drivers is empty or lacks itDriverManager.getDrivers()add a runtime dependency
URL prefix wrongdriver present, still no matchcompare the URL with the tablecorrect the prefix
provided or test scopeworks in tests, fails in the JARinspect the built artifactchange scope to runtime
Split classloaderdriver visible to server, not to the applist drivers from app codeuse the container datasource
Fat JAR without services mergeno drivers at alljar tf app.jar for the services filemerge with ServicesResourceTransformer
Driver too old for URL optionsmatch fails on new parametersdriver version against docsupgrade the driver

Messages that look related but are not:

MessageReal causeWhere to look
Failed to determine a suitable driver classSpring Boot cannot map URL to driverspring.datasource.url and dependency
No suitable driver foundDriverManager has no matchclasspath and URL
Communications link failuredriver connected, server unreachablehost, port, firewall
Public Key Retrieval is not allowedMySQL auth over plain connectionallowPublicKeyRetrieval and TLS
Access denied for usercredentialsgrants and user host

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

1. The URL prefix must match the driver

java
// MariaDB driver does not accept this prefix DriverManager.getConnection("jdbc:mysql://db:3306/shop", user, pass); // No suitable driver found // either use the MariaDB prefix DriverManager.getConnection("jdbc:mariadb://db:3306/shop", user, pass); // or add the MySQL connector

The reverse case is just as common: a jdbc:postgresql: URL with only the MySQL connector on the classpath. Compare the URL character by character; jdbc:postgres:// is a frequent typo that produces this exact exception. Store the URL and credentials in one configuration source rather than in several properties files, and compare environments with the env formatter when dev works and prod does not.

2. Packaging: scope, shading and nested JARs

xml
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/> </transformers> </configuration> </plugin>

Without the services transformer, the shade plugin keeps the first META-INF/services/java.sql.Driver it finds and drops the rest, so a multi-database build registers only one driver. Spring Boot's repackaged JAR keeps nested JARs under BOOT-INF/lib; the driver loads normally there, but code that scans the classpath by file path does not see it.

bash
jar tf target/app.jar | grep -E "services/java.sql.Driver|mysql-connector" unzip -p target/app.jar META-INF/services/java.sql.Driver

3. Application servers and split classloaders

Tomcat, WildFly and WebLogic give each web application its own classloader. A driver placed in the server's lib directory is visible to the server, and the app can use it, but DriverManager filters drivers by the caller's classloader when the caller is a web app. Two workable patterns:

java
// 1. register explicitly with the app's loader at startup Class.forName("com.mysql.cj.jdbc.Driver", true, getClass().getClassLoader()); // 2. or let the container own the pool and inject a DataSource @Resource(lookup = "java:comp/env/jdbc/shop") private DataSource ds;

The second is preferable in an app server: the container manages connections, and the application never touches DriverManager. The context.xml resource declares driverClassName and url once, and the app code asks for the JNDI name.

4. Spring Boot and the "failed to determine" message

spring: datasource: url: jdbc:postgresql://db:5432/shop username: app password: ${DB_PASSWORD}

With that configuration and no org.postgresql:postgresql on the runtime classpath, Boot fails earlier than DriverManager with Failed to determine a suitable driver class. Adding the dependency and restarting converts the error into a connection attempt, which is the progress you want. A test that runs on H2 needs the H2 dependency in test scope, plus a test profile that does not reuse the production URL.

bash
java --module-path mods --add-modules java.sql,com.mysql.cj -cp app.jar Probe

A modular runtime needs java.sql and the driver module resolved explicitly; otherwise the service file is present but never read. In OSGi, each bundle needs its own import of java.sql and a fragment or a DataSourceFactory; relying on the automatic module name of a driver JAR is fragile because it changes between releases.

6. Diagnosing without redeploying

java
System.out.println(System.getProperty("java.class.path")); System.out.println(Probe.class.getClassLoader()); DriverManager.getDrivers().asIterator().forEachRemaining(d -> System.out.println(d));

If the list is empty, the driver is not on the classpath the JVM actually uses. If it contains the driver and the URL still fails, the URL is wrong. Log both once at startup and the next incident takes a minute instead of an afternoon. Checksums of the deployed JARs are worth recording as well; the hash tool produces a SHA-256 for a build artifact that can be compared with CI output.

Common Compilation/Runtime Mistakes & Version Gotchas

Adding Class.forName as the first fix. It only helps when the JAR is present but auto-registration is disabled, which is rare on modern JVMs. Confirm the driver is on the classpath first.

Using provided scope for the driver. The scope means "the container provides it", and quite often nothing does. A shipped application needs runtime.

Keeping the old MySQL driver class name. com.mysql.jdbc.Driver is from Connector/J 5.x. Connector/J 8 and later use com.mysql.cj.jdbc.Driver, and the old name still loads in some versions with a deprecation warning that hides the version mismatch.

Assuming the JVM sees the classpath you exported. CLASSPATH is ignored by java -jar, which uses the JAR manifest. Code that worked under java -cp fails under the launcher script for that reason.

Version notes. Connector/J 9 dropped some legacy behaviours and requires Java 8 or later, with 8.4+ recommended. PostgreSQL JDBC 42.7 changed the default of assumeMinServerVersion and moved to jdbc:postgresql://host:port/db?sslmode=require guidance. Maven's mysql:mysql-connector-java artifact is deprecated in favour of com.mysql:mysql-connector-j, so a copied dependency block may resolve nothing at all.

Reference material: DriverManager and java.sql.Driver.

Official references

primary documentation

The Java documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.