用gradle jar文件查找主类时出现问题

nafvub8i  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(652)

我见过很多人在用gradle制作jar文件时出现“找不到或加载主类”错误。我浏览了很多论坛,还没有找到一个适合我的解决方案。通过一个在线教程,我能够在vscode中建立一个gradle项目,并为一个基本的helloworld程序添加依赖项。我在这里看到的教程链接:https://www.youtube.com/watch?v=bwdkyrnjqsg 这对我来说没问题,但是当我完成了更大的程序时,它找不到我的main.class文件。这是我的build.gradle文件:

plugins {
    id 'java'
    id 'java-library'
    id 'application'
}

application
{
    mainClass = 'Main'
}

java
{
    sourceCompatibility = JavaVersion.VERSION_12
    targetCompatibility = JavaVersion.VERSION_12
}

project.configurations.implementation.setCanBeResolved(true);
version = '1.0.0'
mainClassName = 'Main'

sourceSets
{
    main 
    {
        java
        {
            srcDirs 'src/main/java'
        }
    }
}

repositories
{
    mavenCentral()
    maven
    {
        url = 'https://licensespring-maven.s3.eu-central-1.amazonaws.com/'
    }
}

dependencies
{
    implementation 'com.licensespring:licensespring-license-client:2.1.0'
    // https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java
    implementation group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '4.0.0-alpha-6'
    // https://mvnrepository.com/artifact/net.harawata/appdirs
    implementation group: 'net.harawata', name: 'appdirs', version: '1.2.0'
    // https://mvnrepository.com/artifact/net.java.dev.jna/jna-platform
    implementation group: 'net.java.dev.jna', name: 'jna-platform', version: '5.6.0'
    // https://mvnrepository.com/artifact/net.java.dev.jna/jna
    implementation group: 'net.java.dev.jna', name: 'jna', version: '5.6.0'
    // https://mvnrepository.com/artifact/org.slf4j/slf4j-api
    implementation group: 'org.slf4j', name: 'slf4j-api', version: '2.0.0-alpha1'
    // https://mvnrepository.com/artifact/commons-io/commons-io
    implementation group: 'commons-io', name: 'commons-io', version: '2.7'
}

jar
{
    manifest
    {
        attributes(
            "Main-Class": "$mainClassName",
            "Class-Path": configurations.implementation.collect {it.name}.join(' ')
        )
    }
    dependsOn('dependencies')
    from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
}

src/main/java/ 我有main.java文件和main方法以及所有其他的.java文件。当我跑的时候 gradlew clean build ,我的 .class 文件显示在 build/classes/java/main . 只是想澄清一下,当我跑的时候 java -jar './build/libs/myApplication.jar' 我明白了 Could not find or load main class Main 错误。如果你需要更多的细节,请告诉我。
更新:再次切换到intellij,我得到了涉及主manifest属性的错误。我检查了输出的jar,清单文件确实是打包的,它确实有一个主清单属性。请帮帮我!

4ngedf3f

4ngedf3f1#

你能试着给出main的源代码集和完整的包名吗?应该是这样的:

sourceSets {
    main {
        java {
            srcDirs 'src/main/java'
        }
    }
}

project.configurations.implementation.setCanBeResolved(true)

jar{
    manifest {
        attributes(
                "Main-Class": "com.xyz.Main",
                "Class-Path":  configurations.implementation.collect { it.name }.join(' ')
        )
    }
    dependsOn ('dependencies')
}
kyvafyod

kyvafyod2#

我在创建可运行jar时遇到了类似的问题。我可以用下面的例子来解决这个问题
解决方案1:创建一个包含所有依赖项的胖jar。

task fatJar(type: Jar) {
 manifest {attributes 'Main-Class': 'com.example.gradle.App'}
 from {
       configurations.compile.collect
       { it.isDirectory() ? it : zipTree(it) }
 }with jar
}

解决方案2:创建一个可运行的jar,将依赖项复制到目录并设置类路径

def dependsDir = "${buildDir}/libs/dependencies/"
task copyDependencies(type: Copy) {
   from configurations.compile
   into "${dependsDir}"
}

task createJar(dependsOn: copyDependencies, type: Jar) {
   manifest {
      attributes(
       'Main-Class': 'com.example.gradle.App',
       'Class-Path': configurations.compile.collect
             { 'dependencies/' + it.getName() }.join(' ')
      )}
     with jar
}

阅读更多关于这个例子在这里

相关问题