【问题标题】:java.lang.NoClassDefFoundError: when trying to run jarjava.lang.NoClassDefFoundError:尝试运行 jar 时
【发布时间】:2017-07-16 10:41:37
【问题描述】:

我有一个使用 gradle 的 Spring Boot 项目

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'jetty'
apply plugin: 'war'
apply plugin: 'org.springframework.boot'

repositories {
    mavenCentral()
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web:1.5.1.RELEASE") {
        exclude module: "spring-boot-starter-tomcat"
    }
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-jetty', version: '1.5.1.RELEASE'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-actuator', version: '1.5.1.RELEASE'
    compile group: 'org.springframework', name: 'spring-webmvc', version: '4.3.6.RELEASE'
    compile group: 'org.springframework', name: 'spring-context-support', version: '4.3.6.RELEASE'
    compile group: 'org.springframework', name: 'spring-orm', version: '4.3.6.RELEASE'
    compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
    compile group: 'org.freemarker', name: 'freemarker', version: '2.3.23'
    compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.5'
    compile group: 'org.hibernate', name: 'hibernate-core', version: '5.1.4.Final'
    compile group: 'commons-validator', name: 'commons-validator', version: '1.5.1'
    compile group: 'junit', name: 'junit', version: '4.12'
    compile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '1.5.1.RELEASE'
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.2'
}

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.1.RELEASE")
    }
}

jar {
    baseName = 'base-name'
    version = '0.1.0'
    manifest {
        attributes 'Main-Class': 'some.application.Application'
    }
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

我构建使用
./gradlew clean jar
然后尝试使用
java -jar <jar-name>.jar

运行

但是我得到一个错误提示

Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication
        at some.application.Application.main(Application.java:11)
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)
        ... 1 more

我很困惑为什么我可以在 IDE 中运行它但不能作为 jar 运行。我还发现我的 jar 根本不包含任何库。

编辑:

这是我的Application.class

打包 some.application;

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

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

【问题讨论】:

  • 我会比较IDE使用的路径和jar的内容
  • 你的代码里有springboot入口类吗?
  • @EduardoDennis 编辑的问题显示了我的Application.java。这就是你说的spring boot entry class吗?
  • @efekctive 我刚刚检查了我的罐子,显然它根本不包含任何弹簧靴罐子。这就解释了为什么它不能运行任何东西。你知道我是怎么把弹簧靴罐塞进罐子里的吗?
  • 执行 gradle install 时会发生什么?它没有引入依赖项?

标签: java gradle spring-boot jar build.gradle


【解决方案1】:

这是因为你的依赖没有包含在最终的 jar 文件中。

查看示例 herehere

或者使用./gradlew clean build,它应该会自动正确打包应用程序。看看the official guide

【讨论】:

  • 漂亮。添加from { (configurations.runtime).collect { it.isDirectory() ? it : zipTree(it) } } 对我来说就像一个魅力:) 谢谢!
【解决方案2】:
  1. build.gradle中定义一个名为fatJar的任务:
jar {
    baseName = 'base-name'
    version = '0.1.0'
    manifest {
        attributes(
                'Main-Class': 'com.github:'
        )
    }
}

task fatJar(type: Jar) {
    manifest.from jar.manifest
//    classifier = 'all'
    from {
        configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
    } {
        exclude "META-INF/*.SF"
        exclude "META-INF/*.DSA"
        exclude "META-INF/*.RSA"
    }
    with jar
}

artifacts {
    archives fatJar
}
  1. 运行gradle fatJar./gradlew fatJar 以生成一个包含所有依赖项的jar 文件,该文件可以独立运行,但可能非常大(这就是它被称为fat jar 的原因)。

【讨论】:

    猜你喜欢
    • 2013-11-04
    • 2022-10-02
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多