首页 > 解决方案 > Gradle spring-boot 错误 - java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication

问题描述

下面是我的 build.gradle 文件。我正在使用 java 11 和 IntelliJ 2019.2.2。当我运行应用程序时,我得到:线程“main”中的异常 java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication

因此,错误或问题似乎与您从 gradle 版本 2.x 升级到任何 3.4 及更高版本时遇到的错误或问题相同,但我不应该收到任何最新 IntelliJ 版本的错误。我已经清理了存储库、想法缓存等。主类 (com.itreatmd.emr.Application) 只有一行 - SpringApplication.run(Application.class, args)并引发此异常。

另外,当我从命令行构建 tar 文件然后分解 tar 文件时,我看不到我的 jar 文件打包在 tar 文件的 lib 文件夹下。我显然错过了一些东西,大概是一些明显的东西,但就是无法掌握它。

buildscript {
    ext {
        springBootVersion = '2.2.1.RELEASE'
    }
    repositories {
        jcenter()
        maven { url "http://repo.spring.io/snapshot" }
        maven { url "http://repo.spring.io/milestone" }
    }

    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
    }
}


plugins {
    id 'org.springframework.boot' version '2.2.1.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
    id 'application'
    id 'idea'
}
mainClassName = 'com.itreatmd.emr.Application'
group = 'com.itreatmd.emr'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

jar {
    baseName = 'emrServices'
    version = '1.0-SNAPSHOT'
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-jersey'
    compile group: 'com.google.code.gson', name: 'gson', version: '2.7'
    compile group: 'commons-codec', name: 'commons-codec', version: '1.5'
    compile group: 'commons-io', name: 'commons-io', version: '2.6'
    compile group: 'org.json', name: 'json', version: '20180813'
    compile "org.apache.httpcomponents:httpmime:4.2.3"
    compile group: 'org.yaml', name: 'snakeyaml', version: '1.8'
    compile group: 'org.apache.httpcomponents', name: 'httpmime', version: '4.3.1'
    compile group: 'org.springframework', name: 'spring-webmvc', version: '3.1.1.RELEASE'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

jar {
    manifest {
        attributes 'Main-Class': 'com.itreatmd.emr.Application',
                'Class-Path': configurations.runtime.files.collect { "lib/$it.name" }.join(' ')
    }
}

标签: spring-bootgradleintellij-ideagradle-plugin

解决方案


我正在使用 spring boot 2.2.5 和 gradle 5.1.2。

运行gradle build -x test,它会告诉你这是什么错误。

就我而言,它告诉我error: package org.jasig.cas.client.authentication does not exist,但这个包实际上存在。

然后我记得我添加了一个依赖项runtimeOnly 'org.apereo.cas:cas-server-support-validation:5.3.15.1'

评论此依赖项后,我的项目可以开始成功。


推荐阅读