IntelliJ can't find slf4j logger with gradle

Viewed 15868

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!

1 Answers
Related