Gradle构建依赖抛出ClassNotFoundException [英] Gradle build dependancy throwing ClassNotFoundException

查看:412
本文介绍了Gradle构建依赖抛出ClassNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写我的第一个Gradle构建脚本,以帮助构建一个简单的Java应用程序,以便从命令行使用。



以下是完整的build.gradle代码

  apply plugin: 'java'
apply plugin:'eclipse'

defaultTasks'clean','build'

repositories {
mavenCentral()
}

jar {
baseName ='napier-deploy'
version ='1'
manifest {attributes'Main-Class':'com.Main'}
}

依赖关系{
compile'org.apache.httpcomponents:fluent-hc:4.3.3'
compile'org.apache.httpcomponents:httpclient-cache :4.3.3'
compile'org.apache.httpcomponents:httpcore:4.1'
compile'org.apache.httpcomponents:httpmime:4.3.3'
compile'org.apache.httpcomponents :httpclient:4.3.3'

testCompile'junit:junit:4.11'
testCompile'org.mockito:mockito-all:1.9.5'
}

任务包装器(类型:包装器){
gradleVersion ='1.11'
}

运行gradle驴emble --info命令显示从Maven下载的依赖关系,但是当我尝试从命令行启动Jar时,出现以下错误:

 线程main中的异常java.lang.NoClassDefFoundError:org / apache / http / HttpEntity 
at com.Main.main(Main.java:17)
引起:java.lang .ClassNotFoundException:org.apache.http.HttpEntity $ b $ java.net.URLClassLoader $ 1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
在java.net.URLClassLoader.findClass(URLClassLoader.java:190)$ b $在java.lang.ClassLoader.loadClass(ClassLoader.java:306)
在sun.misc.Launcher $ AppClassLoader.loadClass(启动器.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 1 more

HttpEntity包含在httpcomponents.httpcore工件中,所以我不明白为什么JVM找不到类。

有任何意见。

解决方案

经过一番研究,我已经使用下面的构建脚本来成功构建我的Jar。

  apply plugin:'java'
apply plugin:'eclipse'

defaultTasks'clean','build'

存储库{
mavenCentral()
}

jar {
来自{
configurations.compile.collect {
it .isDirectory()?它:zipTree(it)
}
configurations.runtime.collect {
it.isDirectory()?它:zipTree(它)
}
}
清单{
属性'Implementation-Title':'ndeploy',
'Implementation-Version':'0.1。 0',
'Built-By':System.getProperty('user.name'),
'Built-Date':new Date(),
'Built-JDK':System .getProperty('java.version'),
'Main-Class':'com.Main'
}
}

依赖项{
编译'org.apache.httpcomponents:fluent-hc:4.3.3'
compile'org.apache.httpcomponents:httpclient-cache:4.3.3'
compile'org.apache.httpcomponents:httpcore:4.1 '
compile'org.apache.httpcomponents:httpmime:4.3.3'
compile'org.apache.httpcomponents:httpclient:4.3.3'
testCompile'junit:junit:4.11'
testCompile'org.mockito:mockito-all:1.9.5'
}

buildscript {
dependencies {
classpath fileTree(dir: ../../build/libs',包括:'* .jar',不包括:['* javadoc.jar','* sources.jar'])
}
}

评论赞赏

I am currently writing my first Gradle build script to help structure a simple Java app to be used from the command line.

Below is the full build.gradle code

apply plugin: 'java'
apply plugin: 'eclipse'

defaultTasks 'clean', 'build'

repositories {
    mavenCentral()
}

jar {
baseName = 'napier-deploy'
version =  '1'
manifest {attributes 'Main-Class': 'com.Main'}
}

dependencies {
    compile 'org.apache.httpcomponents:fluent-hc:4.3.3'
    compile 'org.apache.httpcomponents:httpclient-cache:4.3.3'
    compile 'org.apache.httpcomponents:httpcore:4.1'
    compile 'org.apache.httpcomponents:httpmime:4.3.3'
    compile 'org.apache.httpcomponents:httpclient:4.3.3'

    testCompile 'junit:junit:4.11'
    testCompile 'org.mockito:mockito-all:1.9.5'
}

task wrapper(type: Wrapper) {
    gradleVersion = '1.11'
}

Running the gradle assemble --info command shows the dependancies being downloaded from Maven, but when I try to launch the Jar from the commandline I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/HttpEntity
    at com.Main.main(Main.java:17)
Caused by: java.lang.ClassNotFoundException: org.apache.http.HttpEntity
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    ... 1 more

HttpEntity is included in the httpcomponents.httpcore artefact, so I do not see why the JVM is having trouble finding the class.

Any comments are appreciated.

解决方案

After some research I have used the following build script to build my Jar successfully.

apply plugin: 'java'
apply plugin: 'eclipse'

defaultTasks 'clean', 'build'

repositories {
    mavenCentral()
}

jar {
    from {
        configurations.compile.collect {
            it.isDirectory() ? it : zipTree(it)
        }
        configurations.runtime.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
    manifest {
        attributes 'Implementation-Title': 'ndeploy',
            'Implementation-Version': '0.1.0',
            'Built-By': System.getProperty('user.name'),
            'Built-Date': new Date(),
            'Built-JDK': System.getProperty('java.version'),
            'Main-Class': 'com.Main'
    }
}

dependencies {
    compile 'org.apache.httpcomponents:fluent-hc:4.3.3'
    compile 'org.apache.httpcomponents:httpclient-cache:4.3.3'
    compile 'org.apache.httpcomponents:httpcore:4.1'
    compile 'org.apache.httpcomponents:httpmime:4.3.3'
    compile 'org.apache.httpcomponents:httpclient:4.3.3'
    testCompile 'junit:junit:4.11'
    testCompile 'org.mockito:mockito-all:1.9.5'
}

buildscript {
    dependencies {
        classpath fileTree(dir: '../../build/libs', include: '*.jar', excludes: ['*javadoc.jar', '*sources.jar'])
    }
}

Comments appreciated

这篇关于Gradle构建依赖抛出ClassNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆