Spring Boot and Logging - java

I have Spring Boot application with slf4j logging.
Gradle:
buildscript {
ext {
springBootVersion = '2.0.0.BUILD-SNAPSHOT'
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
dependencies {
compile fileTree(dir: 'lib', include: '*.jar')
compile group: 'com.google.guava', name: 'guava', version: '17.0'
// Spring
compile 'org.springframework.boot:spring-boot-starter-web:1.5.1.RELEASE'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-parent', version: '1.5.1.RELEASE'
// Spring Security
compile 'org.springframework.boot:spring-boot-starter-security'
// Template engine
compile 'org.springframework.boot:spring-boot-starter-thymeleaf:1.5.1.RELEASE'
compile 'org.thymeleaf.extras:thymeleaf-extras-springsecurity3:2.1.2.RELEASE'
compile group: 'org.thymeleaf', name: 'thymeleaf-spring5', version: '3.0.3.M1'
// DB and ORM
compile 'org.springframework.boot:spring-boot-starter-data-jpa:1.5.1.RELEASE'
compile 'org.apache.derby:derby:10.13.1.1'
// Form validation
compile 'org.hibernate:hibernate-validator:5.2.2.Final'
compile 'javax.el:el-api:2.2'
// SNMP
compile 'org.snmp4j:snmp4j:1.10.1'
compile 'org.snmp4j:snmp4j-agent:1.2'
testCompile('org.springframework.boot:spring-boot-starter-test')
}
Class:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
.....
#SpringBootApplication
public class MyApplication {
private static final Logger LOGGER = LoggerFactory.getLogger(MyApplication.class);
public static void main(String[] args) {
SpringApplication.run(new Class<?>[] {MyApplication.class}, args);
.....
It worked before. Now I have exception where I create Logger. I didn't change anything, only tried to build project again. Maybe Spring Boot version was changed, I don't know.
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
at by.virkom.MyApplication.<clinit>(MyApplication.java:18)
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:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
I tried to exclude spring-boot-starter-logging and connect
spring-boot-starter-log4j but it not worked for me. Then ClassNotFoundException with Log4j. How can I fix it?
P.S.: When I commented creating logger, I have another exception:
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication
at by.virkom.MyApplication.main(MyApplication.java:22)
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:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more

add this to your gradle file:
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.5'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.5'
compile group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.5'

You have to add an implementation of slf4j Logger like logback
Add the following to your gradle build
compile 'ch.qos.logback:logback-classic:1.1.7'

You have two options:
If your app is a webapp, add spring-boot-starter-web as a dependency and all logging dependencies will be added.
If your app is no webapp, add spring-boot-starter-logging as a dependency.
Source: https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html

Related

NoClassDefFoundError with gradle multi projects

I have setup a gradle multi project in java. The build was successful but when I tried to run the JAR, a java.lang.NoClassDefFoundError was thrown.
Here is my project structure:
.gradle/
authserver/
build/
src/main/java/fr/evywell/robserver/auth/
Main.java
build/
common/
build/
src/main/java/fr/evywell/robserver/common/
gradle/
build.gradle
settings.gradle
And then my configuration:
settings.gradle (IN ROOT DIR)
rootProject.name = "robserver"
include 'authserver'
include 'common'
build.gradle (IN ROOT DIR)
subprojects {
apply plugin: 'java'
repositories {
mavenCentral()
}
}
build.gradle (IN common DIR)
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'io.netty', name: 'netty-all', version: '4.1.24.Final'
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.16'
compile group: 'org.javassist', name: 'javassist', version: '3.25.0-GA'
compile group: 'com.jsoniter', name: 'jsoniter', version: '0.9.23'
}
build.gradle (IN authserver DIR)
apply plugin: 'application'
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'io.netty', name: 'netty-all', version: '4.1.24.Final'
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.16'
compile group: 'org.javassist', name: 'javassist', version: '3.25.0-GA'
compile group: 'com.jsoniter', name: 'jsoniter', version: '0.9.23'
compile project (':common')
}
mainClassName = 'fr.evywell.robserver.auth.Main'
jar {
manifest {
attributes 'Main-Class': 'fr.evywell.robserver.auth.Main'
}
}
Error message:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: fr/evywell/robserver/common/network/Server
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: fr.evywell.robserver.common.network.Server
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)
... 7 more
The error message says that it cant find a class in my common package.
I don't know why because I used compile project (':common') in my authserver gradle configuration
Here is the command: java -jar authserver/build/libs/authserver.jar
Thank you for your help and have a nice day !
For me worked next thing: in kotlin I haved spring boot plugin applied to all subprojects {} in root build.gradle
Instead of this I've removed
apply(plugin = "org.springframework.boot") from root build.gradle and applied this line for each subproject instead of common module
I think the issue is that the JAR you are creating does not include the class files generated from compiling the common module. Try ammending the jar task of authserver module so that you include the output of compiling the common module:
jar {
from project.sourceSets.main.allSource
from project(":common").sourceSets.main.java.output
manifest {
attributes 'Main-Class': 'fr.evywell.robserver.auth.Main'
}
}
I used the shadow plugin to build a "fat jar". I don't know if it's a good practice...
Thank you for your help !
// authserver/build.gradle
plugins {
id 'com.github.johnrengelman.shadow' version '5.1.0'
id 'java'
}
apply plugin: 'application'
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'io.netty', name: 'netty-all', version: '4.1.24.Final'
compile group: 'mysql', name: 'mysql-connector-java', version: '8.0.16'
compile group: 'org.javassist', name: 'javassist', version: '3.25.0-GA'
compile group: 'com.jsoniter', name: 'jsoniter', version: '0.9.23'
compile project (':common')
}
mainClassName = 'fr.evywell.robserver.auth.Main'
jar {
manifest {
attributes 'Main-Class': 'fr.evywell.robserver.auth.Main'
}
}

java.lang.AbstractMethodError: Configuration error when trying to execute test using Spring Boot [duplicate]

I'm a neophyte to Spring Boot, and I wanted to change my default logger to log4j2, since it has a higher throughput rate than logback.
Here's my Gradle script. As you can see, I am using Spring Boot 2.0.3, and to disable the default logger I used exclude module(logback and spring boot starter logger) after Spring Boot Web. I am compiling log4j at the bottom of my script.
buildscript {
ext {
springBootVersion = '2.0.3.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'app'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
jcenter()
}
ext {
vaadinVersion = '8.4.1'
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-rest')
providedCompile 'javax.servlet:javax.servlet-api:3.1.0'
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
compile('org.springframework.boot:spring-boot-starter-web') {
exclude module: "spring-boot-starter-logging"
exclude module: "logback-classic"
}
compile('org.springframework.boot:spring-boot-starter-webflux')
compile('org.springframework.boot:spring-boot-starter-jdbc')
compile('org.springframework.boot:spring-boot-starter-log4j2')
compile('com.vaadin:vaadin-spring-boot-starter')
runtime('org.springframework.boot:spring-boot-devtools')
compileOnly('org.springframework.boot:spring-boot-configuration-processor')
compileOnly('org.projectlombok:lombok')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('io.projectreactor:reactor-test')
testCompile('org.springframework.security:spring-security-test')
testCompile 'junit:junit:4.12'
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.1'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.1'
// https://mvnrepository.com/artifact/com.h2database/h2
//compile group: 'com.h2database', name: 'h2', version: '1.0.60'
testCompile group: 'com.h2database', name: 'h2', version: '1.3.148'
}
dependencyManagement {
imports {
mavenBom "com.vaadin:vaadin-bom:${vaadinVersion}"
}
}
This is my Spring Boot application.
package app.clothapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class ClothappApplication {
public static void main(String[] args) {
SpringApplication.run(ClothappApplication.class, args);
}
}
However, when I run my Spring Boot application, it provides me with an AbstractMethodError. I've read some other SO questions, and apparently some class has inappropriately changed since it was last compiled. Could anyone provide some help?
Exception in thread "main" java.lang.AbstractMethodError: org.apache.logging.log4j.core.config.ConfigurationFactory.getConfiguration(Lorg/apache/logging/log4j/core/config/ConfigurationSource;)Lorg/apache/logging/log4j/core/config/Configuration;
at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:472)
at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:442)
at org.apache.logging.log4j.core.config.ConfigurationFactory.getConfiguration(ConfigurationFactory.java:254)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:419)
at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:138)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:147)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:41)
at org.apache.logging.log4j.LogManager.getContext(LogManager.java:175)
at org.apache.commons.logging.LogFactory$Log4jLog.<clinit>(LogFactory.java:199)
at org.apache.commons.logging.LogFactory$Log4jDelegate.createLog(LogFactory.java:166)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:109)
at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:99)
at org.springframework.boot.SpringApplication.<clinit>(SpringApplication.java:198)
at app.clothapp.ClothappApplication.main(ClothappApplication.java:10)
Thank you.
There will be more than one transitive dependency on spring-boot-starter-logging. For example:
spring-boot-starter-security depends on spring-boot-starter
spring-boot-starter depends on spring-boot-starter-logging
You can run gradle dependencies to confirm.
In order to exlcude a dependency from everywhere, use:
configurations {
all*.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}
Spring suggest including the dependency on spring-boot-starter and then excluding the logger from there, something like:
compile('org.springframework.boot:spring-boot-starter') {
exclude module: "spring-boot-starter-logging"
}
But I have found that not all Spring dependencies are so well behaved, so the above sometimes fails when there are explicit dependencies on Spring logging elsewhere.

IntelliJ can't find slf4j logger with gradle

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.

java.lang.NoClassDefFoundError: when trying to run jar

I have a spring boot project using gradle
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
I build using
./gradlew clean jar
Then try to run using
java -jar <jar-name>.jar
However I get an error saying
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
I am so confused as to why I can run it in IDE but not as a jar. Also I found out that my jar doesn't contain any libraries at all.
EDIT:
This is my Application.class
package 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);
}
}
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
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
}
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).

Intellij / libGDX adding lib kryo NoClassDefFoundError

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'
}
}

Categories

Resources