2017-02-21 81 views
0

我正在使用Gradle在Eclipse中构建我的Java项目。 gradle.build如下所示成功的Gradle项目构建在运行时产生NoClassDefFoundError

apply plugin: 'java' 
repositories { 
    mavenCentral() 
} 
dependencies { 
    compile group: 'com.flowpowered', name: 'flow-nbt', version: '1.0.0' 
    compile group: 'org.reflections', name: 'reflections', version: '0.9.10' 
} 

所有库在运行Eclipse时都能正常运行。但是有时在命令行上工作很有用。在命令行上运行时,运行时错误Exception in thread "main" java.lang.NoClassDefFoundError: com/flowpowered/nbt/regionfile/SimpleRegionFileReader发生,即使构建成功并且代码包含从这些库导入。我尝试了清理和重建,以及gradlew build --refresh-dependencies,但我仍然遇到相同的运行时错误。

我会假设图书馆只是从来没有真正导入?或者,他们没有被存储在Java项目认为他们在哪里?我不熟悉Gradle,所以对此有任何建议都是值得欢迎的。

回答

2

根据发布的build.gradle文件,您并未将应用程序打包为可执行JAR。

首先应用application插件。但是这样做还不够,因为你无法将可执行文件作为单个JAR运行而没有所有的依赖关系。也应用shadow插件。

这两个插件就可以访问下列任务:

  • run:从执行的gradle的命令行应用程序。
  • runShadow:执行应用程序,但将所有依赖关系打包在单个JAR中,以及已编译的类和资源。
  • shadowJar:创建一个具有编译类和所有依赖关系的单个“胖JAR”。

因此您build.gradle可能看起来像这样

plugins { 
    id 'java' 
    id 'application' 
    id 'com.github.johnrengelman.shadow' version '1.2.4' 
} 
mainClassName = 'com.acme.YourMainClassName' 
repositories { 
    mavenCentral() 
} 
dependencies { 
    compile group: 'com.flowpowered', name: 'flow-nbt', version: '1.0.0' 
    compile group: 'org.reflections', name: 'reflections', version: '0.9.10' 
} 

插件文件:

0

另一种解决方案,而使用任何插件,仍然结束与可运行的脂肪罐

jar { 
    archiveName = 'NameOfYourApp.jar' 

    manifest { 
     attributes 'Main-Class': 'uk.co.cdl.Main', 
       'Class-Path': configurations.runtime.files.collect { "lib/$it.name" }.join(' '), 
       'Implementation-Version': project.version 
    } 

    from(configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }) { 
    include/exclude anything if need to if not take the curlys off 
    } 
} 
相关问题