Eclipse在运行或调试期间没有使用gradle依赖jar

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Eclipse在运行或调试期间没有使用gradle依赖jar相关的知识,希望对你有一定的参考价值。

我正在尝试使用Gradle项目执行基本的休眠任务。

依赖项jar由Gradle下载并放置在Project和External Dependencies库中。

enter image description here

我没有得到任何编译时错误。但是当我尝试在Eclipse中运行或调试主类时,我发现类没有找到NoClassDefFoundError。

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

当我检查构建路径时,我可以看到依赖库配置了必需的jar,但是eclipse仍未使用它。

enter image description here

但是当我在构建路径中手动添加jar时,我没有得到这个异常。

enter image description here

Build.gradle文件

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()

提前致谢!!!

答案

我认为你应该使用构建文件中的最新更改来更新类路径。 Eclipse不会在所有版本中自动执行此操作。转到package explorer,右键单击build.gradle文件,然后从上下文菜单中选择gradle-> refresh gradle project。

您还可以从首选项菜单启用自动同步,转到gradle,然后选中“自动项目同步”复选框。

另一答案

删除/评论此依赖项并尝试。

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

我的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()

另一答案

使用eclipse插件,可以添加不同的构建任务:

./gradlew cleanEclipse eclipse

以上是关于Eclipse在运行或调试期间没有使用gradle依赖jar的主要内容,如果未能解决你的问题,请参考以下文章

调试 gradle bootRun 有 server=n

调试 gradle bootRun 有 server=n

无法打开选定的 VM 调试端口 (8700)。确保您没有运行另一个 DDMS 或 eclipse 插件实例

如何在 Eclipse 中使用 Gradle 运行 Cucumber + Spring Boot 应用程序

如何通过 Gradle 测试任务在我的 JUnit 上启用调试

如何在调试期间在 FreeRTOS 任务列表中显示运行时