{"record":{"id":"87101f6bbca94827","repo":"stanfordnlp/CoreNLP","slug":"could-not-find-slf4j-in-your-classpath","errorCode":null,"errorMessage":"Could not find SLF4J in your classpath","messagePattern":"Could not find SLF4J in your classpath","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/util/logging/RedwoodConfiguration.java","lineNumber":236,"sourceCode":"     * Consider using \"output\" instead, unless you really\n     * want to log only to stderr now and forever in the future.\n     */\n    public static final Thunk stderr = (config, root) -> {\n      Redwood.ConsoleHandler handler = Redwood.ConsoleHandler.err();\n      handler.leftMargin = config.channelWidth;\n      root.addChild(handler);\n    };\n\n    /**\n     * Output to slf4j. This is a leaf node.\n     */\n    public static final Thunk slf4j = (config, root) -> {\n      try {\n        OutputHandler handler = MetaClass.create(\"edu.stanford.nlp.util.logging.SLF4JHandler\").createInstance();\n        handler.leftMargin = config.channelWidth;\n        root.addChild(handler);\n      } catch (Exception e) {\n        throw new IllegalStateException(\"Could not find SLF4J in your classpath\", e);\n      }\n    };\n\n    /**\n     * Output to java.util.Logging. This is a leaf node.\n     */\n    public static final Thunk javaUtil = (config, root) -> {\n      try {\n        OutputHandler handler = new JavaUtilLoggingHandler();\n        handler.leftMargin = config.channelWidth;\n        root.addChild(handler);\n      } catch (Exception e) {\n        throw new IllegalStateException(\"Could not find java.util.logging in your classpath\", e);\n      }\n    };\n\n    /**\n     * Output to the default location specified by the output() method.","sourceCodeStart":218,"sourceCodeEnd":254,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/util/logging/RedwoodConfiguration.java#L218-L254","documentation":"Handlers.slff4j Thunk builds an SLF4JHandler via reflective MetaClass.create(\"edu.stanford.nlp.util.logging.SLF4JHandler\"). When instantiation fails (typically ClassNotFoundException because the SLF4J API jar is absent), it rethrows as this IllegalStateException with the original exception as cause.","triggerScenarios":"Using RedwoodConfiguration Handlers.slff4j (e.g. log.handlers including slf4j in applyProperties) without org.slf4j:slf4j-api (and a concrete binding) on the classpath.","commonSituations":"Maven/Gradle project missing the slf4j dependency; fat jar built without SLF4J; runtime classpath (e.g. servlet container or spark-submit) lacking the library that compile-time classpath had.","solutions":["Add SLF4J to the classpath: Maven org.slf4j:slf4j-api plus a binding such as slf4j-simple or logback-classic.","Inspect the cause chain (e.getCause()) to confirm the missing class is SLF4JHandler/SLF4J classes and fix packaging so it is included.","Fall back to Handlers.output/stderr handler if SLF4J is intentionally not a dependency.","If using a shaded jar, verify edu/stanford/nlp/util/logging/SLF4JHandler.class and slf4j classes survived the shading."],"exampleFix":"// before (no slf4j dependency)\nRedwoodConfiguration.empty().handlers(Handlers.slf4j).apply();\n// after: add to pom.xml\n// <dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>2.0.x</version></dependency>\nRedwoodConfiguration.empty().handlers(Handlers.slf4j).apply();","handlingStrategy":"fallback","validationCode":"private static boolean slf4jAvailable() {\n  try { Class.forName(\"org.slf4j.Logger\"); return true; }\n  catch (ClassNotFoundException e) { return false; }\n}","typeGuard":null,"tryCatchPattern":"try {\n  config.handlers(Handlers.slf4j).apply();\n} catch (IllegalStateException e) {\n  if (e.getMessage().contains(\"Could not find SLF4J\")) {\n    config.handlers(Handlers.stderr).apply(); // graceful fallback\n  } else throw e;\n}","preventionTips":["Declare org.slf4j:slf4j-api plus a binding as runtime dependencies whenever Handlers.slf4j is used.","Run a Class.forName(\"org.slf4j.Logger\") availability check at startup.","Verify shaded/fat jars include both SLF4JHandler and SLF4J classes."],"tags":["logging","slf4j","classpath","missing-dependency"],"backgroundTag":"missing-dependency","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}