我想通過gradle創建並運行可執行的jar。這是我目前gradle這個樣子:如何創建然後在Intellij IDE中運行帶有依賴項的可執行jar文件,特別是Android Studio?
task jarTask(type: Jar) {
baseName = 'my-main-class'
from 'build/classes/main'
}
task createJarWithDependencies(type: Jar) {
manifest {
attributes 'Implementation-Title': 'Sample Jar',
'Implementation-Version': 1,
'Main-Class':'com.example.MyMainClass'
}
baseName = "my-main-class-with-dependencies"
from {
configurations.compile.collect {
it.isDirectory() ? it : zipTree(it)
}
}
with jarTask
}
configurations {
jarConfiguration
}
artifacts {
jarConfiguration jarTask
}
// This is the task that I call with ./gradlew to execute my jar
task runMyJar(type: JavaExec) {
classpath files('build/libs/my-main-class-with-dependencies.jar')
main 'com.example.MyMainClass'
args = ["param1","param2"]
outputs.upToDateWhen { false }
}
runMyJar.dependsOn(createJarWithDependencies, build)
我從下面的堆棧這些做法溢出答案/下面引用:然而,當我運行./gradlew clean runMyJar
(甚至只是./gradlew runMyJar
Android Studio export jar with dependencies Android studio - How to use an executable jar file from gradle
,我得到以下錯誤信息:
Error: Could not find or load main class com.example.MyMainClass
任何人都可以指出爲什麼我的可執行jar沒有找到我班裏的主要方法?有什麼我失蹤?
您可以添加您的Main類嗎? –
可能重複的[Gradle-沒有主清單屬性](https://stackoverflow.com/questions/32567167/gradle-no-main-manifest-attribute) – jrtapsell
不完全是你的問題的答案,但看看https:/ /docs.gradle.org/current/userguide/application_plugin.html –