摇篮& Groovy-错误:无法找到或加载主类 [英] Gradle & Groovy - Error: Could not find or load main class
问题描述
我可以使用gradle run
运行我的项目,但是我不能使用java -jar
运行jar文件.我使用以下示例项目重新创建了错误:链接到GitHub上的项目
I can run my project using gradle run
, but I can't run the jar file using java -jar
. I've recreated the error with this sample project: link to project on GitHub
这是通过gradlew
$ ./gradlew run
> Task :run
Hello world.
BUILD SUCCESSFUL in 4s
这是运行项目java -jar
$ ./gradlew build
BUILD SUCCESSFUL in 6s
$ java -jar build/libs/emailer.jar
Error: Could not find or load main class us.company.emailer.App
但是当我unzip
jar
时,我可以看到App.class
But when I unzip
the jar
, I can see App.class
user@computer:../libs$ unzip emailer.jar
Archive: emailer.jar
creating: META-INF/
inflating: META-INF/MANIFEST.MF
creating: us/
creating: us/company/
creating: us/company/emailer/
inflating: us/company/emailer/App.class
这是build.gradle
plugins {
id 'groovy'
id 'application'
}
repositories {
jcenter()
mavenCentral()
}
dependencies {
implementation 'org.codehaus.groovy:groovy-all:2.5.6'
testImplementation 'org.spockframework:spock-core:1.2-groovy-2.5'
compile 'org.apache.commons:commons-email:1.5'
}
mainClassName = 'us.company.emailer.App'
jar {
manifest {
attributes(
'Class-Path': configurations.compile.collect { it.getName() }.join(' '),
'Main-Class': 'us.company.emailer.App'
)
}
}
sourceSets.main.java.srcDirs = ['src/main/groovy']
这是App.groovy
package us.company.emailer
class App {
String getGreeting() {
return 'Hello world.'
}
static void main(String[] args) {
println new App().greeting
}
}
添加MANIFEST.MF
以响应@tkruse的评论
Adding MANIFEST.MF
in response to the comment from @tkruse
Manifest-Version: 1.0
Class-Path: commons-email-1.5.jar javax.mail-1.5.6.jar activation-1.1.
jar
Main-Class: us.company.emailer.App
推荐答案
问题是类路径.如果您查看META-INF/MANIFEST.mf
文件的内部,则可以看到它已设置为:
The problem is the classpath. If you look inside the META-INF/MANIFEST.mf
file, you can see it's set to:
Class-Path: commons-email-1.5.jar javax.mail-1.5.6.jar activation-1.1.
jar
当Java运行时,它不知道这些东西在哪里,它还需要groovy运行时才能理解您的groovy代码.
When java runs, it has no idea where any of these things are, it also requires the groovy runtime in order to understand your groovy code.
执行此操作的最简单方法是将所有依赖项捆绑到" fat-jar "中,而使用Gradle执行此操作的最简单方法是出色的
The simplest way of doing this is to bundle all your dependencies into a "fat-jar", and the simplest way of doing this with Gradle is the excellent Shadow-jar plugin.
如果将以下内容添加到build.gradle
中的plugins
块中:
If you add the following to your plugins
block in build.gradle
:
id 'com.github.johnrengelman.shadow' version '5.0.0'
(您可以删除jar
块和操纵sourceSets的行)
(You can delete the jar
block and the line that manipulates the sourceSets)
然后运行./gradlew shadowJar
您将获得一个jar文件emailer-all.jar
You'll get a jar file emailer-all.jar
可以运行哪个:
$ java -jar build/libs/emailer-all.jar
Hello world.
为完整起见,这是完整的build.gradle文件:
For completeness, here's the complete build.gradle file:
plugins {
id 'groovy'
id 'application'
id 'com.github.johnrengelman.shadow' version '5.0.0'
}
repositories {
jcenter()
mavenCentral()
}
dependencies {
implementation 'org.codehaus.groovy:groovy-all:2.5.6'
testImplementation 'org.spockframework:spock-core:1.2-groovy-2.5'
implementation 'org.apache.commons:commons-email:1.5'
}
mainClassName = 'us.company.emailer.App'
这篇关于摇篮& Groovy-错误:无法找到或加载主类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!