.jar 文件中没有主要清单属性

No main manifest attribute in .jar file

我一直在查看几个不同的线程,但没有找到解决方案。我做了一个项目,它使用 java 和 gradle 从 google 表中检索数据。我创建了一个工件并通过 intelliJ 构建了 jar 文件。当我尝试使用 "java -jar filename.jar" 通过终端 运行 罐子时,我得到 "no main manifest attribute, in filename.jar"

我的 MANIFEST.MF 位于 main/java/META-INF 并具有以下简单结构

Manifest-Version: 1.0
Main-Class: Main

build.gradle 文件具有以下结构:

plugins {
    id 'java'
}

group  'filename'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile 'com.google.api-client:google-api-client:1.30.4'
    compile 'com.google.oauth-client:google-oauth-client-jetty:1.30.4'
    compile 'com.google.apis:google-api-services-sheets:v4-rev581-1.25.0'
}

很明显,我已经在 class 中说明了主要方法是什么,这就是为什么这条错误消息对我来说毫无意义。有人对此有解决方案吗?

我已按照表格中的步骤进行操作 API quickstart to set up the application. After that, I followed the steps from this other answer to generate the .jar file, that explains that you should declare your manifest file inside your build.gradle file within the jar 属性。

在此之后,当 运行 生成的 .jar 文件时,我得到了一个“无效的签名文件”错误,我可以通过在 jar 属性 中添加下面的行来修复这个错误 属性 =15=]:

exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'

生成 .jar 文件后,您可以在项目的 build/libs 目录中找到它。我的最终 build.gradle 文件如下所示:

apply plugin: 'java'
apply plugin: 'application'

group 'egs'
version '1.0-SNAPSHOT'

mainClassName = 'SheetsQuickstart'
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile 'com.google.api-client:google-api-client:1.30.4'
    compile 'com.google.oauth-client:google-oauth-client-jetty:1.30.4'
    compile 'com.google.apis:google-api-services-sheets:v4-rev581-1.25.0'
}

jar {
    manifest {
        attributes 'Main-Class': 'SheetsQuickstart'
    }
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
    exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'
}