0

I am having the following dependencies in my Gradle file:

compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.21'
compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.1.7'
compile group: 'ch.qos.logback', name: 'logback-core', version: '1.1.7'

However when I try to use it for example

private static final Logger LOGGER = (Logger) org.slf4j.LoggerFactory.getLogger(Something.class);

It throws me this error:

java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory

Anyone knows what is going on?

Hong Wei Wang
  • 1,388
  • 3
  • 19
  • 29

2 Answers2

0

You can use lombok with gradle by adding the following to your build.gradle in the dependencies block:

provided "org.projectlombok:lombok:1.16.8"

and just annotate the class with @Slf4j . For more information , visit https://projectlombok.org/api/lombok/extern/slf4j/Slf4j.html

Fahad Fazil
  • 638
  • 1
  • 9
  • 20
0

It appears that what you think is in your classpath doesn't quite match what's happening at runtime. The fact that you're fully qualifying org.slf4j.LoggerFactory and then trying to cast the result to a Logger (presumably from a different package) is also suspicious.

  1. Check your gradle dependencies and ensure that that you're actually using the versions of slf4j-api and logback that you expect to be, and that you're not also using other versions or other logging frameworks that you're not expecting.
  2. Make sure that whatever tool you're using to run your application (presumably your IDE) is correctly reading your Gradle dependencies and using them.
  3. Ensure that your logging imports in your class are from org.slf4j. You shouldn't be importing anything directly from Logback unless you're doing something odd that's Logback-specific and not available via SLF4J (which would be rather unusual). You should be able to just use:

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    …
    private static final Logger LOGGER = LoggerFactory.getLogger(Something.class);