Dustin Hodges
Dustin Hodges

Reputation: 4195

ClassNotFoundException with Spring Boot and ibm JDBC driver

I'm trying to set up a spring-boot application to connect to a DB2 database on an IBM i machine. I've added a maven dependency to an IBM jdbc driver com.ibm.as400.access.AS400JDBCDriver and added spring.datasource.driver-class-name="com.ibm.as400.access.AS400JDBCDriver" to the application.properties file.

When I run the application, I get an error Cannot load driver class: "com.ibm.as400.access.AS400JDBCDriver. The error happens at the ClassUtils.forName line below in the DataSourceProperties class from spring-boot in org.springframework.boot.autoconfigure.jdbc

private boolean driverClassIsLoadable() {
    try {
        ClassUtils.forName(this.driverClassName, null); <----- Exception happens here.  
        return true;
    }
    catch (UnsupportedClassVersionError ex) {
        // Driver library has been compiled with a later JDK, propagate error
        throw ex;
    }
    catch (Throwable ex) {
        return false;
    }
}

this.driverClassName equals "com.ibm.as400.access.AS400JDBCDriver". Interestingly, in my main method, I can call the same function with the same driverClassName and get no exception.

@SpringBootApplication
public class MinimumExampleApplication {

public static void main(String[] args) throws ClassNotFoundException, LinkageError {
    ClassUtils.forName("com.ibm.as400.access.AS400JDBCDriver", null); <---- No exception here?
    SpringApplication.run(MinimumExampleApplication.class, args);
  }
}

My question is why is spring-boot unable to find the driver when I can find the driver if I load it myself?

Full Program

MinimumExample.java

package org.mystuff.annual.meeting.signup;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.util.ClassUtils;

@SpringBootApplication
public class MinimumExampleApplication {

  public static void main(String[] args) throws ClassNotFoundException,    LinkageError {
    ClassUtils.forName("com.ibm.as400.access.AS400JDBCDriver", null);
    SpringApplication.run(MinimumExampleApplication.class, args);
  }
}

application.properties

spring.datasource.driver-class-name="com.ibm.as400.access.AS400JDBCDriver"
spring.datasource.url="jdbc:db2://myserver/myDb"
spring.datasource.dbcp2.initial-size=2
spring.datasource.dbcp2.max-total=5

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.mystuff</groupId>
    <artifactId>annual-meeting-signup</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>minimum-example</name>
    <description>Playing with data repository</description>

    <parent>
        <groupId>org.mystuff</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.ibm.as400</groupId>
            <artifactId>jt400</artifactId>
            <version>8</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

Upvotes: 2

Views: 3549

Answers (1)

Dustin Hodges
Dustin Hodges

Reputation: 4195

As @Andy Wilkinson mentioned, the problem was caused by the quotes in my application.properties file. I had it configured like this

spring.datasource.driver-class-name="com.ibm.as400.access.AS400JDBCDriver"
spring.datasource.url="jdbc:db2://myserver/myDb"
spring.datasource.dbcp2.initial-size=2
spring.datasource.dbcp2.max-total=5

If I change to the below it works

spring.datasource.driver-class-name=com.ibm.as400.access.AS400JDBCDriver
spring.datasource.url=jdbc:db2://myserver/myDb
spring.datasource.dbcp2.initial-size=2
spring.datasource.dbcp2.max-total=5

Upvotes: 1

Related Questions