java.lang.ClassNotFoundException build exception after upgrading to JDK11

Environment

Issue

After upgrading to Java JDK 11 jobs executing Groovy code which were reliying on installed extensions (JAR files in the lib/ext directory of the Java Runtime Environment) are failing with java.lang.ClassNotFoundException.

Example:

import groovy.sql.Sql
def driver = "com.mysql.cj.jdbc.Driver"
def user = "sample_user"
def password = "sample_password"
def url = "jdbc:mysql://sample-host.com"
def db = Sql.newInstance(url, user, password, driver)

It throws a java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver exeception.

Explanation

The lib/ext directory was removed starting in JDK 9, and the presence of this directory will force java or javac executables to exit. Check Java Platform, Standard Edition Oracle JDK 9 Migration Guide: Removed Extension Mechanism for more details about it.

Resolution

You now need to add any third-party JAR files to the application’s classpath:

  • The build classpath if there are compile time dependencies
  • The runtime classpath if there are runtime dependencies

Have more questions?

0 Comments

Please sign in to leave a comment.