简体   繁体   English

Gradle&Groovy - 错误:无法找到或加载主类

[英]Gradle & Groovy - Error: Could not find or load main class

I can run my project using gradle run , but I can't run the jar file using java -jar . 我可以使用gradle run运行我的项目,但是我无法使用java -jar运行jar文件。 I've recreated the error with this sample project: link to project on GitHub 我用这个示例项目重新创建了错误: 链接到GitHub上的项目

This is the output from running the project via gradlew 这是通过gradlew运行项目的输出

$ ./gradlew run

> Task :run
Hello world.

BUILD SUCCESSFUL in 4s

This is the output from running the project java -jar 这是运行项目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

But when I unzip the jar , I can see App.class 但是当我unzip jar ,我可以看到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

Here's the build.gradle 这是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']

Here's the App.groovy 这是App.groovy

package us.company.emailer

class App {

    String getGreeting() {
        return 'Hello world.'
    }

    static void main(String[] args) {
        println new App().greeting
    }
}

EDIT: Adding MANIFEST.MF in response to the comment from @tkruse 编辑:添加MANIFEST.MF以响应@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

The problem is the classpath. 问题是类路径。 If you look inside the META-INF/MANIFEST.mf file, you can see it's set to: 如果您查看META-INF/MANIFEST.mf文件,您可以看到它设置为:

Class-Path: commons-email-1.5.jar javax.mail-1.5.6.jar activation-1.1.
 jar

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. 当java运行时,它不知道这些东西在哪里,它还需要groovy运行时才能理解你的groovy代码。

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 . 最简单的方法是将所有依赖项捆绑到一个“ fat-jar ”中,使用Gradle执行此操作的最简单方法是出色的Shadow-jar插件

If you add the following to your plugins block in build.gradle : 如果将以下内容添加到build.gradleplugins块中:

    id 'com.github.johnrengelman.shadow' version '5.0.0'

(You can delete the jar block and the line that manipulates the sourceSets) (您可以删除jar块和操作sourceSets的行)

Then run ./gradlew shadowJar 然后运行./gradlew shadowJar

You'll get a jar file emailer-all.jar 你会得到一个jar文件emailer-all.jar

Which can be run: 哪个可以运行:

$ java -jar build/libs/emailer-all.jar
Hello world.

For completeness, here's the complete build.gradle file: 为完整起见,这是完整的build.gradle文件:

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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM