Eclipse is not using gradle dependencies jar during run or debug

Viewed 2606

I am trying to do basic hibernate task using Gradle project.

Dependency jars are download by Gradle and placed in Project and External Dependencies library.

enter image description here

I don't get any compile time error. But when I try to run or debug the main class in Eclipse, I am getting class not found NoClassDefFoundError.

Exception in thread "main" java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration

When I checked the build path, I could see the dependency library is configured with required jars but still eclipse is not using it.

enter image description here

But when I add the jar manually in the build path, I am not getting this exception.

enter image description here

Build.gradle File

plugins {
    // Apply the java-library plugin to add support for Java Library
    id 'java-library'
}

apply plugin: "eclipse"

dependencies {
    // This dependency is exported to consumers, that is to say found on their compile classpath.
    api 'org.apache.commons:commons-math3:3.6.1'

    // This dependency is used internally, and not exposed to consumers on their own compile classpath.
    implementation 'com.google.guava:guava:23.0'

    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'

    // https://mvnrepository.com/artifact/org.hibernate/hibernate-core
    compile group: 'org.hibernate', name: 'hibernate-core', version: '5.3.6.Final'

    // https://mvnrepository.com/artifact/com.oracle.jdbc/ojdbc6
    runtime group: 'com.oracle.jdbc', name: 'ojdbc6', version: '11.1.0.6.0'

    // https://mvnrepository.com/artifact/com.oracle/ojdbc6
    runtime group: 'com.oracle', name: 'ojdbc6', version: '11.2.0.4.0-atlassian-hosted'




}

// In this section you declare where to find the dependencies of your project
repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
    mavenCentral()
}

Thanks in advance!!!

3 Answers

I think you should update the classpath with the latest changes in the build file. Eclipse does not do that automatically in all versions. Go to package explorer, right-click the build.gradle file, then from the context menu select gradle->refresh gradle project.

You can also enable auto sync from the preferences menus, go to gradle, and check the "Automatic Project Synchronization" checkbox.

Remove/comment this dependency and try.

runtime group: 'com.oracle.jdbc', name: 'ojdbc6', version: '11.1.0.6.0' 

My Build.gradle:

plugins {
    // Apply the java-library plugin to add support for Java Library
    id 'java-library'
}

dependencies {
    // This dependency is exported to consumers, that is to say found on their compile classpath.
    api 'org.apache.commons:commons-math3:3.6.1'

    // This dependency is used internally, and not exposed to consumers on their own compile classpath.
    implementation 'com.google.guava:guava:23.0'

    // Use JUnit test framework
    testImplementation 'junit:junit:4.12'

     // https://mvnrepository.com/artifact/org.hibernate/hibernate-core
    compile group: 'org.hibernate', name: 'hibernate-core', version: '5.3.6.Final'

    // https://mvnrepository.com/artifact/com.oracle.jdbc/ojdbc6
   // runtime group: 'com.oracle.jdbc', name: 'ojdbc6', version: '11.1.0.6.0'

    // https://mvnrepository.com/artifact/com.oracle/ojdbc6
    runtime group: 'com.oracle', name: 'ojdbc6', version: '11.2.0.4.0-atlassian-hosted'

}

// In this section you declare where to find the dependencies of your project
repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

With the eclipse plugin, there are different build tasks being added:

./gradlew cleanEclipse eclipse
Related