1

I'm trying to build and run a Spark webserver that uses slf4j logging in IntelliJ. It builds fine (also in IntelliJ) on the computer that was used to originally add logging, but not on my machine.

Here's the full build.gradle file (see project(':webserver') for the part that uses the logger:

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

Here's the code that uses the logger:

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));

        });
    }

}

And here's the error I get:

"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

I've looked at several questions on StackOverflow, but none seem to solve the problem:

I've tried the following dependencies:

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

None of this seems to make a difference. Any ideas?

Community
  • 1
  • 1
Leon Overweel
  • 1,488
  • 4
  • 17
  • 27
  • Looks like you don't have the slf4j-api in your class path during execution. – Henry May 22 '17 at 11:51
  • @Henry how do I add it? Seems like I shouldn't have to download it and put it somewhere if I'm using gradle, right? Shouldn't it take care of that? Thanks – Leon Overweel May 22 '17 at 12:04
  • Try to add slf4j-api to the compile dependencies, use the same version you used for slf4j-log4j12. – Henry May 22 '17 at 12:10
  • @Henry I changed it to `compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.6.1'` and `compile group: 'org.slf4j', name: 'slf4j-api', version: '1.6.1'`, refreshed gradle, and tried to run the server again, but I got the same error. – Leon Overweel May 22 '17 at 12:17
  • The server is started by gradle or in another way? – Henry May 22 '17 at 12:18
  • I start it by just clicking the play button next to the server's `main` method. – Leon Overweel May 22 '17 at 12:32

2 Answers2

3

If you're using Gradle add these 2 lines to your build.gradle dependencies to use Spark 2.6.0

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

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

Ben Lewis
  • 213
  • 1
  • 9
  • He tried a number of approaches and this is to clarify that (assuming a clean cache) is the most concise way to do it as well as has the version number for slf4j that is found on Spark (2.6.0) official documentation http://sparkjava.com/documentation#examples-and-faq – Ben Lewis Aug 14 '17 at 03:00
0

I ended up deleting my Gradle cache (.gradle folder) and re-downloading everything, which solved the problem.

Leon Overweel
  • 1,488
  • 4
  • 17
  • 27