Classpath for jdbc drivers - Cannot find class: org.postgresql.Driver

I’m using mybatis, and follow configuration:

, and when i’m trying to run JUnit test Exception appear: Cause: java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: Cannot find class: org.postgresql.Driver

This is my subproject build.gradle

apply plugin: “java”

buildscript {

repositories {

flatDir {

dirs “/home/tdzierza/Programy/architect-1.0.7/jdbc” // This is proper path

}

}

dependencies {

classpath group: ‘org.postgresql’, name: ‘postgresql’, version: ‘9.3-1102-jdbc41’

} }

/* And this is other case - not working buildscript {

dependencies {

classpath fileTree(dir: ‘/home/tdzierza/Programy/architect-1.0.7/jdbc’,

includes: [‘postgresql-8.2-506.jdbc3.jar’])

} } */

/* And this is other case - not working buildscript {

repositories {

mavenCentral()

mavenLocal()

}

dependencies {

classpath ‘org.postgresql:postgresql:9.3-1102-jdbc41’

} } */

dependencies {

compile group: ‘commons-collections’, name: ‘commons-collections’, version: ‘3.2’

compile group: ‘org.mybatis’, name: ‘mybatis’, version: ‘3.+’

compile group: ‘org.wildfly’, name: ‘wildfly-ejb3’, version: ‘8.1.0.Final’

testCompile group: ‘junit’, name: ‘junit’, version: ‘4.+’ }

/* I added this to check classpath after build - and acctualy i can’t find jdbc there */ jar {

manifest {

attributes(

“Implementation-Title”: project.name,

“Implementation-Version”: version,

“Class-Path”: configurations.compile.collect { it.getName() }.join(’ '))

} }

You need to put the JDBC driver on the ‘compile’ or ‘runtime’ class path (in the same block where the ‘mybatis’ dependency is declared), not on the buildscript class path. The latter would only be needed if the build script itself were to access the database.

Yes, that’s the point.

Unfortunately it’s still not working directly from netbeans but everything is ok when i run in CLI.

Thanks +1.