Gradle生成的启动脚本找不到主java类

问题描述 投票:0回答:1

Gradle生成的启动脚本运行时找不到主类:

./ApplicationName.bat
Error: Could not find or load main class packageName.NameOfMainClass

我的 build.gradle 文件看起来像这样:

/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Java application project to get you started.
 * For more details take a look at the 'Building Java & JVM projects' chapter in the Gradle
 * User Manual available at https://docs.gradle.org/6.8.2/userguide/building_java_projects.html
 */

plugins {
    // Create jar file
    id 'java'

    // Apply the application plugin to add support for building a CLI application in Java.
    id 'application'
}

repositories {
    // Use JCenter for resolving dependencies.
    jcenter()
    // Maven
    mavenCentral()
}

def jmockitVersion = '1.49'

dependencies {
    // JMockit
    testImplementation "org.jmockit:jmockit:$jmockitVersion"
    
    // Use JUnit test framework.
    testImplementation 'junit:junit:4.13'

    // This dependency is used by the application.
    implementation 'com.google.guava:guava:29.0-jre'
    
    //other dependencies not shown
    .
    .
    .
}

test {
    jvmArgs "-javaagent:${classpath.find { it.name.contains("jmockit") }.absolutePath}"
}

application {
    // Define the main class for the application.
    mainClass = 'packageName.NameOfMainClass'
}


jar {
   archivesBaseName = 'ApplicationName'
   archiveVersion = '0.0.1-SNAPSHOT'
   manifest {
        attributes 'Implementation-Title': 'Gradle Quickstart',
                   'Implementation-Version': version,
                   'Main-Class': 'packageName.NameOfMainClass'
    }
}

run {
   standardInput(System.in)
}

生成的 jar 的 MANIFEST.MF 如下所示:

Manifest-Version: 1.0
Implementation-Title: Gradle Quickstart
Implementation-Version: 0.0.1-SNAPSHOT
Main-Class: packageName.NameOfMainClass

文件结构是这样的:

applicationName\
    gradlew
    gradlew.bat
    settings.gradle
    README.md
    .gradle\
    gradle\
    applicationName\
        build.gradle
        build\
        src\
            main\
                java\
                    packageName\
                        NameOfMainClass.java
                        OtherClass.java
                        .
                        .
                        .
                resources\
            test\
                java\
                    packageName\
                        SomeTest.java
                        .
                        .
                        .
                resources\

我是 Gradle 新手。我在其他帖子中尝试过一些东西,可能相似也可能不相似,但没有成功。

java gradle build.gradle
1个回答
0
投票

Gradle 的启动脚本设计为在应用程序安装中运行(即作为 distribution 的一部分)。否则,脚本中指定的路径是错误的。

所以,如果你第一次运行:

./gradlew installDist

然后你应该在

./build/install/applicationName/bin/applicationName.bat
处有一个工作脚本,以Gradle项目目录为根目录。

© www.soinside.com 2019 - 2024. All rights reserved.