I'm testing out REST using Jersey, Hibernate and embedded Jetty. Initially, I was able to start everything up using ./gradlew clean jar && java -jar build/libs/<project>.jar. However, I wanted to support JSON and read from docs that MOXy is Jersey's default JSON provider. So I added the following in my build.gradle file:
compile 'org.glassfish.jersey.media:jersey-media-moxy:2.19'
After which, I tried to build the jar and start up jetty but I get the following issue:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
at sun.security.util.SignatureFileVerifier.processImpl(SignatureFileVerifier.java:284)
at sun.security.util.SignatureFileVerifier.process(SignatureFileVerifier.java:238)
at java.util.jar.JarVerifier.processEntry(JarVerifier.java:273)
at java.util.jar.JarVerifier.update(JarVerifier.java:228)
at java.util.jar.JarFile.initializeVerifier(JarFile.java:383)
at java.util.jar.JarFile.getInputStream(JarFile.java:450)
at sun.misc.URLClassPath$JarLoader$2.getInputStream(URLClassPath.java:940)
at sun.misc.Resource.cachedInputStream(Resource.java:77)
at sun.misc.Resource.getByteBuffer(Resource.java:160)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:454)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
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)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:495)
Here's my build.gradle file if it helps.
apply plugin: 'eclipse'
apply plugin: 'java'
apply plugin: 'jetty'
repositories {
jcenter()
}
dependencies {
compile 'org.slf4j:slf4j-api:1.7.12'
// Jetty
compile 'org.eclipse.jetty:jetty-server:9.3.1.v20150714'
compile 'org.eclipse.jetty:jetty-servlet:9.3.1.v20150714'
// Jersey
compile 'org.glassfish.jersey.containers:jersey-container-servlet:2.19'
compile 'org.glassfish.jersey.core:jersey-client:2.19'
compile 'org.glassfish.jersey.media:jersey-media-moxy:2.19'
compile 'org.glassfish.jersey.media:jersey-media-json-processing:2.19'
compile 'org.glassfish.jersey.media:jersey-media-multipart:2.19'
compile 'org.glassfish.jersey.media:jersey-media-sse:2.19'
// Hibernate
compile 'mysql:mysql-connector-java:5.1.36'
compile 'org.hibernate:hibernate-core:4.3.10.Final'
compile 'com.mchange:c3p0:0.9.5.1'
testCompile 'junit:junit:4.12'
}
jar {
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
manifest {
attributes 'Main-Class': 'com.domain.scaffolding.Main'
}
}
After looking a bit more into how gradle builds the fat jar, what solved the issue was to fix jar block:
jar {
from(configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }) {
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
}
manifest {
attributes 'Main-Class': 'com.domain.scaffolding.Main'
}
}
Related
When I build my jar, I get ClassNotFoundException for AutoConfigurationReportLoggingInitializer.
This class is not in Spring libraries built by Gradle and also the class is not necessary for running in IDE. Is it possible to disable searching for this class? Or do you have some other solution?
EXCEPTION:
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at org.springframework.util.ClassUtils.forName(ClassUtils.java:284)
at org.springframework.boot.SpringApplication.createSpringFactoriesInstances(SpringApplication.java:438)
... 10 more
build.gradle:
plugins {
id 'org.springframework.boot' version '2.3.3.RELEASE'
id 'io.spring.dependency-management' version '1.0.8.RELEASE'
id 'java'
}
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.1'
}
Try adding the spring-boot-starter dependency which includes spring-boot-starter-logging which has the class you are missing
i have been trying for several hours to get my java program to run via commandline using "java -jar myFile.jar arg1 arg2 ...", but i ONLY get this error when running via command line.
Additionally the program has a UI interface (with Swing), which runs without problems.
Stacktrace:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/poi/ss/usermodel/Row
at CSV2Excel.NewJFrame.main(NewJFrame.java:467)
Caused by: java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Row
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
Gradle:
apply plugin: 'java'
apply plugin: 'jacoco'
apply plugin: 'application'
mainClassName = 'CSV2Excel.NewJFrame'
jar {
manifest {
attributes(
'Class-Path': configurations.compile.collect { $it.getName() }.join(' '),
'Main-Class': 'CSV2Excel.NewJFrame',
)
}
}
repositories {
jcenter()
}
dependencies {
testImplementation 'junit:junit:4.13'
implementation 'org.json:json:20200518'
implementation 'org.apache.poi:poi-excelant:4.1.2'
implementation 'com.opencsv:opencsv:5.3'
}
If I specify the individual jars instead of "configurations.compile.collect { $it.getName() }.join(' ')" in the Classpath, I get the error that no Main-Class attribute is stored.
I am a complete beginner when it comes to Gradle.
Thank you very much.
Edit:
i am using netbeans to build my project, which gives me a zip file with a bin and lib directory with all necessary libs in it.
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
I have a project for which I use gradle, and I wanted to use the logger slf4j provides.
Running in Android Studio produces the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at com.mypkg.Main.<clinit>(Main.java:9)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
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
My build.gradle for the project:
task wrapper(type: Wrapper) {
gradleVersion = '3.5'
}
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.0'
}
}
apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'java'
apply plugin: 'application'
jar {
manifest {
attributes 'Main-Class': "Main"
}
}
shadowJar {
mergeServiceFiles('META-INF/spring.*')
}
mainClassName = 'com.mypkg.Main'
dependencies {
compile 'com.squareup:otto:1.3.5'
compile 'com.fasterxml.jackson.core:jackson-databind:2.4.4'
compile 'com.squareup.okhttp:okhttp:2.7.4'
compile 'com.squareup.okhttp:okhttp-ws:2.7.4'
compile 'com.parse.bolts:bolts-android:1.2.1'
compile 'org.json:json:20140107'
compile 'org.bouncycastle:bcprov-jdk15on:1.52'
compile 'commons-codec:commons-codec:1.10'
compile 'org.scream3r:jssc:2.8.0'
compile 'ch.qos.logback:logback-classic:1.1.3'
compile 'ch.qos.logback:logback-core:1.1.3'
compile 'org.json:json:20140107'
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.0.1'
compile group: 'org.springframework.shell', name: 'spring-shell', version: '1.2.0.RELEASE'
compile group: 'org.springframework', name: 'spring-context-support', version: '4.2.4.RELEASE'
compile group: 'org.springframework', name: 'spring-core', version: '4.2.4.RELEASE'
compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.25'
}
sourceCompatibility = "1.8"
targetCompatibility = "1.8"
If I build this project with gradle, the shadow plugin generates a fat jar, which runs from the console nicely, it includes the logger, but from the IDE I'm having difficulties finding the LoggerFactory. I read here that I should add the logger to the classpath but since I don't explicitly include a jar, rather than marking it as a dependency, I'm not sure how this would be possible.
Please advise.
Cheers!
The classpath is out of sync, few options here
Refresh the project in IntelliJ (the older versions of IntelliJ are not ideal with gradle integration)
add apply plugin: 'idea' to your build.gradle and then run gradle idea to regenerate .iml .ipr .iws files in your project and pick up dependencies from gradle.(Not ideal! see why)
Download latest version of IntelliJ and redo option 1
Edit: Thanks #CrazyCoder for this hint.
I'm trying to add the lib from esotericsoftware "Kryo" to my libGDX project on the Desktop and Android module. I'm using Intellij.
What I tried:
Adding the kryo-3.0 folder to the External Libraries
Adding the dependencies in build.gradle to all modules
compile "com.esotericsoftware:kryo:3.0.3"
Run the Gradle
Sync Project.
What I got after compile the Desktop module:
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: java.lang.NoClassDefFoundError: org/objenesis/strategy/InstantiatorStrategy
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:131)
Caused by: java.lang.NoClassDefFoundError: org/objenesis/strategy/InstantiatorStrategy
at com.projectbeta.deepdarkness.screens.MenuScreen.show(MenuScreen.java:18)
at com.badlogic.gdx.Game.setScreen(Game.java:61)
at com.projectbeta.deepdarkness.DeepDarkness.create(DeepDarkness.java:16)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:147)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:124)
Caused by: java.lang.ClassNotFoundException: org.objenesis.strategy.InstantiatorStrategy
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)
... 5 more
Just tested and it works, paste that into root gradle.build of your project (not in some of the module specific) and resync.
project(":core") {
apply plugin: "java"
dependencies {
......
compile group: 'com.esotericsoftware', name: 'kryo', version: '3.0.3'
}
}
project(":desktop") {
apply plugin: "java"
dependencies {
compile project(":core")
.......
compile group: 'com.esotericsoftware', name: 'kryo', version: '3.0.3'
}
}
project(":android") {
apply plugin: "android"
configurations { natives }
dependencies {
compile project(":core")
......
compile group: 'com.esotericsoftware', name: 'kryo', version: '3.0.3'
}
}