简体   繁体   English

Spring Gradle构建罐无法通过NoClassDefFoundError找到主类

[英]Spring gradle build jar cannot find main class with NoClassDefFoundError

I build jar file in spring framework test code with gradle. 我用gradle在spring框架测试代码中构建jar文件。

and I run jar file "java -jar jarFile.jar" but it can't run with some error code. 并且我运行了jar文件“ java -jar jarFile.jar”,但是它无法运行并显示一些错误代码。

I checked Main-Class attributes in spring project's build.gradle file And MANIFEST.FM file in jar file 我在spring项目的build.gradle文件中检查了Main-Class属性,在jar文件中检查了MANIFEST.FM文件

this is my Main class java Code 这是我的主类Java代码

package com.apress.springrecipes.sequence;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.apress.springrecipes.sequence.config.SequenceConfiguration;

public class Main {

    public static void main(String[] args) {

        ApplicationContext context =
                new AnnotationConfigApplicationContext(SequenceConfiguration.class);

        SequenceGenerator generator = context.getBean(SequenceGenerator.class);

        System.out.println(generator.getSequence());
        System.out.println(generator.getSequence());
    }
}

and this is my build.gradle file 这是我的build.gradle文件

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'application'

mainClassName = "com.apress.springrecipes.sequence.Main"

repositories {
    mavenCentral()
}

jar {
    baseName = "${rootProject.name}"
    version =  "0.0.1-SNAPSHOT"
    manifest {
        attributes "Implementation-Title": "${rootProject.name}",
                   "Implementation-Version": version,
                   "Main-Class": "${mainClassName}"
    }
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

ext {
    springVersion = '4.2.0.RELEASE'
}

[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

dependencies {
    compile "org.springframework:spring-core:${springVersion}"
    compile "org.springframework:spring-context:${springVersion}"
    compile "org.slf4j:slf4j-simple:1.7.25"
}

this is MANIFEST.FM file in jar file 这是jar文件中的MANIFEST.FM文件

Manifest-Version: 1.0
Implementation-Title: recipe_2_2
Implementation-Version: 0.0.1-SNAPSHOT
Main-Class: com.apress.springrecipes.shop.Main

when i run jar file i get error message 当我运行jar文件时,出现错误消息

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/context/ApplicationContext
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
    at java.lang.Class.getMethod0(Class.java:3018)
    at java.lang.Class.getMethod(Class.java:1784)
    at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.springframework.context.ApplicationContext
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 7 more

how can i fix it ? 我该如何解决?

the gradle 'application' plugin will output the zip file in build/distributions , so you can extract into a folder, it have bin/, lib/ gradle'application'插件将以build / distributions输出zip文件,因此您可以将其解压缩到一个文件夹中,该文件夹具有bin /,lib /

you can run your program just like 您可以像运行程序一样

bin/${rootProject.name}

please don't run your jar file in the lib directly 请不要直接在lib中运行jar文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM