title: Keycloak Example with Mysql datasource - Caused by: java.lang.ClassNotFoundException: Could not load requested class : com.mysql.cj.jdbc.Driver
body: <p>I am trying to master Keycloak Custom User Storage SPI with the example provide in the following link <a href="https://github.com/keycloak/keycloak-quickstarts/tree/latest/user-storage-simple" rel="nofollow noreferrer">https://github.com/keycloak/keycloak-quickstarts/tree/latest/user-storage-simple</a></p>
  
  <p>I have used Hibernate to load com.mysql.cj.jdbc.Driver in order to configure the datasource in my persistence.xml file</p>
  
  <pre><code>&lt;persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"&gt;
      &lt;persistence-unit name="keycloak-spi-v1" transaction-type="RESOURCE_LOCAL"&gt;
      &lt;class&gt;com.patrickhub.ch.keycloakspiv1.entity.User&lt;/class&gt;
          &lt;properties&gt;
              &lt;property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/&gt;
              &lt;property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/keycloakspidb?useSSL=false&amp;amp;allowPublicKeyRetrieval=true"/&gt;
              &lt;property name="javax.persistence.jdbc.user" value="username"/&gt;
              &lt;property name="javax.persistence.jdbc.password" value="password"/&gt;
              &lt;property name="javax.persistence.schema-generation.database.action" value="none"/&gt;
              &lt;property name="javax.persistence.schema-generation.scripts.action" value="none"/&gt;
          &lt;/properties&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p>I used entityManager EntityManagerFactory class to get EntityManager as the transaction-type equal to RESOURCE_LOCAL.</p>
  
  <pre><code>private EntityManager entityManager = Persistence.createEntityManagerFactory("keycloak-spi-v1").createEntityManager()
  </code></pre>
  
  <p>I have also declared  java-sql-connection dependencies and others as follow</p>
  
  <pre><code>&lt;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 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt;
    &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  &lt;groupId&gt;ch.patrickhub.com&lt;/groupId&gt;
    &lt;artifactId&gt;keycloak-spi-v1&lt;/artifactId&gt;
    &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
    &lt;packaging&gt;jar&lt;/packaging&gt;
    &lt;description&gt; &lt;/description&gt;
  &lt;dependencies&gt;
        &lt;dependency&gt;
          &lt;groupId&gt;mysql&lt;/groupId&gt;
          &lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
          &lt;version&gt;8.0.16&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
           &lt;groupId&gt;org.keycloak&lt;/groupId&gt;
           &lt;artifactId&gt;keycloak-core&lt;/artifactId&gt;
           &lt;version&gt;8.0.1&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
           &lt;groupId&gt;org.keycloak&lt;/groupId&gt;
           &lt;artifactId&gt;keycloak-server-spi&lt;/artifactId&gt;
           &lt;version&gt;8.0.1&lt;/version&gt;
        &lt;/dependency&gt;
        &lt;dependency&gt;
           &lt;groupId&gt;org.jboss.logging&lt;/groupId&gt;
           &lt;artifactId&gt;jboss-logging&lt;/artifactId&gt;
           &lt;version&gt;3.3.2.Final&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
            &lt;groupId&gt;javax&lt;/groupId&gt;
            &lt;artifactId&gt;javaee-api&lt;/artifactId&gt;
            &lt;version&gt;8.0&lt;/version&gt;
          &lt;/dependency&gt;
   &lt;/dependencies&gt;
  &lt;build&gt;
      &lt;sourceDirectory&gt;src&lt;/sourceDirectory&gt;
      &lt;plugins&gt;
        &lt;plugin&gt;
          &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
          &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
          &lt;version&gt;3.8.0&lt;/version&gt;
          &lt;configuration&gt;
            &lt;release&gt;11&lt;/release&gt;
          &lt;/configuration&gt;
        &lt;/plugin&gt;
        &lt;plugin&gt;
          &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
              &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
          &lt;version&gt;3.2.0&lt;/version&gt;
        &lt;/plugin&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  
  </code></pre>
  
  <p>When I run <code>mvn clean package</code>, everything build find and I even succeed to deploy into keycloak and create a User federation with my jar.
  But when i try to view all users i get this error:</p>
  
  <pre><code>Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [com.mysql.cj.jdbc.Driver]
          at org.hibernate@5.3.13.Final//org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:134)
          at org.hibernate@5.3.13.Final//org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.loadDriverIfPossible(DriverManagerConnectionProviderImpl.java:163)
          at org.hibernate@5.3.13.Final//org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildCreator(DriverManagerConnectionProviderImpl.java:119)
          at org.hibernate@5.3.13.Final//org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.buildPool(DriverManagerConnectionProviderImpl.java:103)
          at org.hibernate@5.3.13.Final//org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:75)
          at org.hibernate@5.3.13.Final//org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:100)
          at org.hibernate@5.3.13.Final//org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:246)
          at org.hibernate@5.3.13.Final//org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:214)
          at org.hibernate@5.3.13.Final//org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.buildJdbcConnectionAccess(JdbcEnvironmentInitiator.java:145)
          at org.hibernate@5.3.13.Final//org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:66)
          at org.hibernate@5.3.13.Final//org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35)
          at org.hibernate@5.3.13.Final//org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:94)
          at org.hibernate@5.3.13.Final//org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263)
          ... 172 more
  Caused by: java.lang.ClassNotFoundException: Could not load requested class : com.mysql.cj.jdbc.Driver
          at org.hibernate@5.3.13.Final//org.hibernate.boot.registry.classloading.internal.AggregatedClassLoader.findClass(AggregatedClassLoader.java:210)
          at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
          at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
          at java.base/java.lang.Class.forName0(Native Method)
          at java.base/java.lang.Class.forName(Class.java:398)
          at org.hibernate@5.3.13.Final//org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:131)
          ... 184 more
  
  </code></pre>
  
  <p>Please can anyone help ?</p>