使用Gradle找不到slf4j LoggerFactory类定义

时间:2017-05-22 11:41:59

标签: maven intellij-idea gradle slf4j

我正在尝试构建并运行一个在IntelliJ中使用slf4j日志记录的Spark网络服务器。它在用于最初添加日志记录的计算机上构建良好(也在IntelliJ中),但不在我的机器上构建。

这里是完整的build.gradle文件(有关使用记录器的部分,请参阅project(':webserver')

buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "gradle.plugin.com.avast.gradle:docker-compose-gradle-plugin:0.3.27"
    }
}

subprojects {
    repositories {
        mavenLocal()
        mavenCentral()
        jcenter()
    }

    apply plugin: "java"
    apply plugin: "idea"
    apply plugin: "checkstyle"
    apply plugin: "pmd"
    apply plugin: "findbugs"
    apply plugin: "application"
    apply plugin: "com.avast.gradle.docker-compose"

    checkstyle {
        configFile = "../config/checkstyle/checkstyle.xml" as File
        toolVersion = "6.0"
    }
}

project(':webserver') {
    mainClassName = "org.passport.webserver.WebServer"
    dependencies {
        compile project(':core')
        compile "com.sparkjava:spark-core:2.6.0"
        testCompile 'junit:junit:4.12'
        compile group: 'org.json', name: 'json', version: '20090211'
        compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.6.1'
    }

    sourceSets {
        main {
            java {
                srcDir 'src/main/java/'
            }
        }
    }
}

project(':customsUI') {
    mainClassName = "org.passport.customsui.CustomsUI"
    dependencies {
        compile project(':core')
        testCompile 'junit:junit:4.12'
    }

    sourceSets {
        main {
            java {
                srcDir 'src/main/java/'
            }
        }
    }
}

project(':handlerUI') {
    mainClassName = "org.passport.handlerui.HandlerUI"
    dependencies {
        compile project(':core')
        testCompile 'junit:junit:4.12'
    }

    sourceSets {
        main {
            java {
                srcDir 'src/main/java/'
            }
        }
    }
}

project(':core') {
    mainClassName = "org.passport.core.PassPortChaincode"
    dependencies {
        compile 'io.grpc:grpc-all:0.13.2'
        compile 'commons-cli:commons-cli:1.3.1'
        compile 'org.glassfish:javax.json:1.1.0-M1'
        compile files('lib/shim-client-1.0.jar')
        testCompile 'junit:junit:4.12'
    }

    sourceSets {
        main {
            java {
                srcDir 'src/main/java/'
            }
        }
    }
}

以下是使用记录器的代码:

package org.passport.webserver;

import org.passport.webserver.endpoints.ContainerClaimDao;
import org.passport.webserver.endpoints.PackageClaimDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static spark.Spark.before;
import static spark.Spark.get;
import static spark.Spark.path;
import static spark.Spark.post;

/**
 * The server matches requests (routs) and calls the appropriate endpoints.
 */
public final class Server {

    private static Logger logger = LoggerFactory.getLogger(Server.class);
    private static ContainerClaimDao containers = new ContainerClaimDao();
    private static PackageClaimDao packages = new PackageClaimDao();

    private Server() {

    }

    /**
     * Starts the router.
     * @param args run arguments
     */
    public static void main(String[] args) {
        path("/containers/:container-id", () -> {
            before("/*", (q, a) -> logger.info("Received api call: " + q.url() + "."));

            post("", (request, response) -> containers.add(request, response));

            get("/container-claims", (request, response) -> containers.get(request, response));

            get("/package-claims", (request, response) ->   packages.get(request, response));

        });
    }

}

这是我得到的错误:

"C:\Program Files\Java\jdk1.8.0_91\bin\java" -Didea.launcher.port=7534 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.2.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_91\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_91\jre\lib\rt.jar;C:\Users\lover\repositories\passport\webserver\build\classes\main;C:\Program Files (x86)\JetBrains\IntelliJ IDEA 2016.2.4\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain org.passport.webserver.Server
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
    at org.passport.webserver.Server.<clinit>(Server.java:18)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:123)
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)
    ... 4 more

我已经看过StackOverflow上的几个问题,但似乎都没有解决问题:

我尝试过以下依赖项:

  • compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.6.1'
  • compile 'org.slf4j:slf4j-api:1.7.24'compile 'org.slf4j:slf4j-simple:1.7.24'
  • compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.6.1'

这些似乎没有任何区别。有什么想法吗?

2 个答案:

答案 0 :(得分:1)

如果您正在使用Gradle,请将这两行添加到build.gradle依赖项中以使用Spark 2.6.0

compile "com.sparkjava:spark-core:2.6.0"

compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.21'

答案 1 :(得分:0)

我最终删除了我的Gradle缓存(.gradle文件夹)并重新下载了所有内容,从而解决了这个问题。