【问题标题】:Gradle Spring Boot Project Cannot Run From IDEA [duplicate]Gradle Spring Boot项目无法从IDEA运行[重复]
【发布时间】:2017-07-21 13:13:48
【问题描述】:

我正在努力设置我在 intelliJ IDEA 2016 1.4 中导入的 spring boot 项目。 每次我跑。我得到以下异常:

    Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication
        at com.patientConnect.DemoApplication.main(DemoApplication.java:24)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
    Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 6 more

我的 gradle 设置(链接的 gradle 项目、用户 gradle 包装器)看起来都不错。

package com.Demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public DemoApplication() {
    }

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }


}

build.gradle:

buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
        maven {
            url 'https://plugins.gradle.org/m2/'
        }
    }
    dependencies {
        classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.4.2.RELEASE'
        classpath 'gradle.plugin.com.jamesward:atom-gradle-plugin:0.0.1'
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'    

repositories {
    mavenLocal()
    mavenCentral()
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-parent:1.5.4.RELEASE'
    compile 'org.springframework.boot:spring-boot-starter-web'
    compile 'org.springframework.boot:spring-boot-devtools'
    compile 'org.springframework.boot:spring-boot-starter-security'
    compile 'org.springframework.security.oauth:spring-security-oauth2'

    compile 'org.apache.httpcomponents:httpclient'
    compile 'org.springframework.boot:spring-boot-starter-data-jpa'
    compile 'org.postgresql:postgresql'
    compile 'org.springframework.boot:spring-boot-starter-jdbc'
    compile 'org.hibernate:hibernate-entitymanager'


}

allprojects {
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
}



import org.gradle.internal.os.OperatingSystem

task devClasses(type: Exec) {
    if (OperatingSystem.current().isWindows())
        commandLine 'gradlew', '-t', 'classes'
    else
        commandLine './gradlew', '-t', 'classes'
}

task devBootRun(type: Exec) {
    if (OperatingSystem.current().isWindows())
        commandLine 'gradlew', 'bootRun'
    else
        commandLine './gradlew', 'bootRun' //, '--debug-jvm'
}

import java.util.concurrent.*

task dev() << {
    def devClassesFuture = Executors.newSingleThreadExecutor().submit({ devClasses.execute() } as Callable)
    def devBootRunFuture = Executors.newSingleThreadExecutor().submit({ devBootRun.execute() } as Callable)
    devClassesFuture?.get()
    devBootRunFuture?.get()
}


task stage {
    dependsOn build
}

【问题讨论】:

    标签: spring-boot intellij-idea gradle


    【解决方案1】:

    尝试删除您计算机上的 SpringBoot 依赖项。

    它将位于 ~/.gradle/caches/modules-2/files-2.1/org.springframework.boot 假设您将 gradle 依赖项保存在默认位置。

    rm -rf ~/.gradle/caches/modules-2/files-2.1/org.springframework.boot
    

    或删除所有依赖项(只是为了确保没有其他依赖项被损坏)

    rm -rf ~/.gradle/caches/modules-2/files-2.1/*
    

    然后重新下载

    gradle build --refresh-dependencies
    

    【讨论】:

    • 试过了。不工作。还是一样的问题:(
    • 通过命令行编译jar会不会产生同样的错误? gradle build
    • 当我做 gradlew build 。它的成功。但是当我从命令行执行 gradlew bootrun 时,它再次给出相同的结果
    • 尝试stackoverflow.com/questions/42587487/… 修复。它不工作
    • 嗯,缩小了问题的范围。您可以发布您的主要课程(尽管我认为原因不在那里),您的 build.gradle。您也可以尝试使用此网站start.spring.io 创建示例测试项目并添加一些依赖项,例如 DevTools (SpringBoot)、Web 等。确保您使用该站点生成 Gradle 项目并尝试使用 intellij 运行它
    猜你喜欢
    • 2016-11-29
    • 2020-04-06
    • 2016-12-30
    • 2017-08-12
    • 1970-01-01
    • 2020-07-21
    • 1970-01-01
    • 1970-01-01
    • 2018-09-29
    相关资源
    最近更新 更多