java - Jar 文件可以从 Gradle Run 运行,但在 Eclipse 中运行时出现 NoClassDefFoundError

标签 java eclipse gradle jar

我使用 Gradle 构建了一个 jar 文件。它在“gradle run”中运行良好,但是当我从 Eclipse 中运行它时(运行->外部工具->外部工具配置),我在其中一个 Spring 类上遇到了 NoClassDefFoundError。

这是build.gradle:

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'
mainClassName = 'com.ii.mainClass'

repositories {
    mavenCentral()
 }

jar {
     manifest {
         attributes 'Main-Class': 'com.ii.mainClass'
    }
 }
 sourceCompatibility = 1.7
 targetCompatibility = 1.7

 dependencies {
      compile 'com.fasterxml.jackson.core:jackson-annotations:2.6.1'
      compile 'com.fasterxml.jackson.core:jackson-core:2.6.1'
      compile 'com.fasterxml.jackson.core:jackson-databind:2.6.1'
      compile 'org.springframework:spring-beans:4.2.0.RELEASE'
      compile 'org.springframework:spring-context:4.2.0.RELEASE'
      compile 'org.springframework:spring-core:4.2.0.RELEASE'
      compile 'org.springframework:spring-expression:4.2.0.RELEASE'
      compile 'org.springframework.xd:spring-xd-tuple:1.0.4.RELEASE'
   }

我查看了 jar 文件,它没有依赖的 jar,这就是我在 Eclipse 中运行时出现 NoClassDefinition 错误的原因。我也通过在本地包含这些 jar 进行了尝试,但这并没有解决问题,有人可以让我知道如何在 Eclipse 中运行吗?

  dependencies {   
       compile fileTree(dir: 'lib', include: ['*.jar'])
   }

最佳答案

当您向 build.gradle 添加依赖项时,您需要从命令行 gradle eclipse 将新的 JAR 文件放入 .classpath,即 eclipse 项目配置。如果您在执行此操作时运行了 eclipse,则需要刷新项目,您将看到新的依赖项出现。可能有一个用于 eclipse 的插件或允许您在 IDE 中完成整个舞蹈的东西。

关于java - Jar 文件可以从 Gradle Run 运行,但在 Eclipse 中运行时出现 NoClassDefFoundError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32387158/

相关文章:

java - 无法理解 "Null type mismatch"

java - 未报告的异常 java.sql.SQLException;必须被捕获或宣布被抛出?

java - 在 IntelliJ 中调试(重新加载更改的类)

android - 我在使Android Studio和gradle正常工作以将我的应用制作到android应用 bundle 时遇到麻烦

java - Gradle:如何让 JavaExec 任务使用配置类路径?

java - Gradle JavaFX - java.lang.NullPointerException : Location is required

Java:在编译时或应用程序执行时运行测试

java - Xamarin - 我应该使用哪些 NDK 版本?

java - 如何在 MAT Eclipse 中使用 OQL 进行嵌入式查询

java - 如何在 Eclipse 插件中使用 Timer 和 SwingWorker?