我试图运行带有依赖项的jar。应用程序在Intellij中运行良好,但是当我尝试运行jar时,我得到:
>> java -jar test-0.1.0.jar
Exception in thread "main" java.lang.NoClassDefFoundError: com/atlassian/httpclient/apache/httpcomponents/DefaultHttpClient
at com.atlassian.jira.rest.client.internal.async.AsynchronousHttpClientFactory.createClient(AsynchronousHttpClientFactory.java:53)
at com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory.create(AsynchronousJiraRestClientFactory.java:35)
at com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory.createWithBasicHttpAuthentication(AsynchronousJiraRestClientFactory.java:42)
at Main.main(Main.java:19)
Caused by: java.lang.ClassNotFoundException: com.atlassian.httpclient.apache.httpcomponents.DefaultHttpClient
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 4 more
我的build.gradle看起来像这样:
group 'test'
version '1.0-SNAPSHOT'
task wrapper(type: Wrapper) {
gradleVersion = '2.9'
distributionUrl = "https://services.gradle.org/distributions/gradle-$gradleVersion-all.zip"
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'application'
sourceCompatibility = 1.8
mainClassName = "Main"
repositories {
mavenCentral()
maven {
url 'https://maven.atlassian.com/content/repositories/atlassian-public/'
}
}
jar {
baseName = 'test'
version = '0.1.0'
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
}
manifest {
attributes 'Main-Class': 'Main'
}
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile 'com.atlassian.jira:jira-rest-java-client-parent:4.0.0'
compile 'com.atlassian.jira:jira-rest-java-client-core:4.0.0'
compile 'com.atlassian.jira:jira-rest-java-client-api:4.0.0'
compile 'com.atlassian.jira:jira-rest-java-client-plugin:3.0.0'
compile 'com.atlassian.fugue:fugue-parent:2.6.1'
compile 'com.atlassian.fugue:fugue:2.6.1'
}
我也尝试在每个编译条款中添加transitive=true
无效。
答案 0 :(得分:0)
类路径是Java运行时环境搜索类和其他资源文件的路径。可以在调用JDK工具(首选方法)时使用-classpath选项或通过设置CLASSPATH环境变量来设置类搜索路径(更常见的是较短名称“类路径”)。 -classpath选项是首选,因为您可以为每个应用程序单独设置它,而不会影响其他应用程序,也不会修改其值的其他应用程序。
<强>更新强>
默认情况下,Gradle不会将依赖项复制到构建文件夹或将其包含在jar中。有三种方法可以解决依赖关系的问题:
例如
task copyDependencies(type: Copy) {
into "$buildDir/libs"
from configurations.runtime
}