I build jar file in spring framework test code with gradle.
and I run jar file "java -jar jarFile.jar" but it can't run with some error code.
I checked Main-Class attributes in spring project's build.gradle file And MANIFEST.FM file in jar file
this is my Main class java Code
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
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
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
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/
you can run your program just like
bin/${rootProject.name}
please don't run your jar file in the lib directly
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.