java IntelliJ 找不到带有 gradle 的 slf4j 记录器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44325205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:06:24  来源:igfitidea点击:

IntelliJ can't find slf4j logger with gradle

javaandroid-studioslf4j

提问by pszent

I have a project for which I use gradle, and I wanted to use the logger slf4j provides.

我有一个使用 gradle 的项目,我想使用 slf4j 提供的记录器。

Running in Android Studio produces the following error:

在 Android Studio 中运行会产生以下错误:

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:

我的项目 build.gradle:

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.Hymanson.core:Hymanson-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.Hymanson.core', name: 'Hymanson-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.

如果我用 gradle 构建这个项目,shadow 插件会生成一个胖 jar,它可以很好地从控制台运行,它包括记录器,但是从 IDE 中我很难找到 LoggerFactory。我在这里读到我应该将记录器添加到类路径中,但由于我没有明确包含一个 jar,而不是将其标记为依赖项,我不确定这怎么可能。

Please advise. Cheers!

请指教。干杯!

采纳答案by LazerBanana

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 ideato regenerate .iml.ipr.iwsfiles in your project and pick up dependencies from gradle.(Not ideal!see why)
  • Download latest version of IntelliJ and redo option 1
  • 在 IntelliJ 中刷新项目(旧版本的 IntelliJ 与 gradle 集成并不理想)
  • 添加apply plugin: 'idea'到您的 build.gradle 中,然后运行gradle idea.iml.ipr.iws在您的项目中重新生成文件并从 gradle 中获取依赖项。(不理想!明白为什么
  • 下载最新版本的 IntelliJ 并重做选项 1

Edit: Thanks @CrazyCoder for this hint.

编辑:感谢@CrazyCoder 提供此提示。