java.lang.NoClassDefFoundError: when trying to run jar

前端 未结 2 2010
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-11 18:51

I have a spring boot project using gradle

apply plugin: \'java\'
apply plugin: \'idea\'
apply plugin: \'jetty\'
apply plugin: \'war\'
apply plugin: \'org.spr         


        
相关标签:
2条回答
  • 2021-01-11 19:41
    1. Define a task called fatJar in build.gradle:
    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. Run gradle fatJar or ./gradlew fatJar to generate a jar file with all dependencies, which can run independently but may be pretty large in size (that's why it's called a fat jar).
    0 讨论(0)
  • 2021-01-11 19:52

    This is because your dependencies are not included into the end jar file.

    Take a look on the examples here or here.

    Or alternatively use ./gradlew clean build that should automatically pack the app correctly. Take a look on the official guide

    0 讨论(0)
提交回复
热议问题