【问题标题】:Spring Boot executable - classnotfound error for main application classSpring Boot 可执行文件 - 主应用程序类的 classnotfound 错误
【发布时间】:2020-03-08 09:16:45
【问题描述】:

下面是我的 build.gradle - 我没有在https://start.spring.io/ 生成的样板代码上编写任何 java 代码 - 我的问题是,在我分解分发文件夹下生成的 tar 文件并运行可执行 jar 之后(使用可以在分发文件夹中找到的脚本),我得到主应用程序类的 classnotfound 异常:

错误:无法找到或加载主类 com.itreatmd.emr.emrServices.EmrServicesApplication 引起:java.lang.ClassNotFoundException:com.itreatmd.emr.emrServices.EmrServicesApplication

我查看了 lib 文件夹,确实在其中找到了我的胖罐子。如果我打开胖 jar 来搜索主应用程序类,我发现它被列为:BOOT-INF/classes/com/itreatmd/emr/emrServices/EmrServicesApplication.class

但是,我没有看到任何 Spring Boot 加载程序类。在肥罐里。

我想我需要修改我的 build.gradle 来纠正这个问题,但我找不到解决方案。

    plugins {
            id 'org.springframework.boot' version '2.2.2.BUILD-SNAPSHOT'
            id 'io.spring.dependency-management' version '1.0.8.RELEASE'
            id 'java'
            id 'distribution'
            id 'application'
    }

    group = 'com.itreatmd.emr'
    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = '11'

    mainClassName = 'com.itreatmd.emr.emrServices.EmrServicesApplication'
    repositories {
            mavenCentral()
            maven { url 'https://repo.spring.io/milestone' }
            maven { url 'https://repo.spring.io/snapshot' }
    }

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter'
    implementation 'org.springframework.boot:spring-boot-loader'
    implementation 'org.springframework.boot:spring-boot-starter-jersey'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}    
    bootJar {
        manifest {
            attributes 'Implementation-Title': 'Gradle Quickstart',
                       'Implementation-Version': version,
                       'Main-Class': 'com.itreatmd.emr.emrServices.EmrServicesApplication'
        }
    }
    test {
            useJUnitPlatform()
    }

【问题讨论】:

  • 为什么要直接从发行版运行 jar 文件?应用程序插件创建的发行版可以为应该用于运行应用程序的 Windows 和类 Unix 平台编写脚本。
  • 是的,我正在分发中使用该脚本来运行应用程序。我没有直接运行 jar。
  • Andy,您知道脚本如何从 BOOT-INF/classes 加载类,因为在我的情况下它基本上无法做到这一点?我没有更改构建在分发文件夹中生成的脚本。
  • 您要使用哪个发行版? Spring Boot 的插件应该create one named boot 包含fat jar。
  • fat jar的内容包括:META-INF/ META-INF/MANIFEST.MF BOOT-INF/ BOOT-INF/classes/ BOOT-INF/classes/com/ BOOT-INF/classes /com/itreatmd/ BOOT-INF/classes/com/itreatmd/emr/ BOOT-INF/classes/com/itreatmd/emr/emrServices/ BOOT-INF/classes/com/itreatmd/emr/emrServices/EmrServicesApplication.class BOOT- INF/lib/ ... BOOT-INF/lib/spring-boot-starter-2.2.1.RELEASE.jar BOOT-INF/lib/spring-boot-loader-2.2.1.RELEASE.jar BOOT-INF/lib /spring-boot-2.2.1.RELEASE.jar ....

标签: spring-boot gradle jar


【解决方案1】:

在最近的 SprintBoot 版本中,我的问题的答案是 build/distribution 文件夹是无用的。当我从旧的 SpringBoot 版本迁移到最近的 2.x 版本时,我认为我可以继续从 build/distribution 文件夹中的 tar 中提取 jars 并继续(在打包和分配)。但是,您在分解分发的 tar 时发现的脚本已损坏。因此,只需使用 bootScripts 下的脚本即可继续。

【讨论】:

    猜你喜欢
    • 2020-03-26
    • 2017-06-17
    • 2018-06-01
    • 1970-01-01
    • 2019-05-16
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多