Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting classpath for slf4j for java compiling

I am having a problem setting up the classpath for slf4j for compiling java files. I tried two ways: 1. provide the classpath in command line

javac -cp /Users/page/.m2/repository/org/slf4j/slf4j-log4j12/1.7.5/slf4j-log4j12-1.7.5.jar src/main/java/com/scg/domain/*.java src/main/java/com/scg/util/*.java

This gave the following error:

src/main/java/com/scg/util/ListFactory.java:8: error: package org.slf4j does not exist
import org.slf4j.Logger;
                ^
src/main/java/com/scg/util/ListFactory.java:9: error: package org.slf4j does not exist
import org.slf4j.LoggerFactory;
...../long error message
  1. I tried to export the CLASSPATH to my env variable.

export CLASSPATH=/Users/page/.m2/repository/org/slf4j/slf4j-log4j12/1.7.5/slf4j-log4j12-1.7.5.jar

This did not help either and resulted in same error, when I tried

javac src/main/java/com/scg/domain/*.java src/main/java/com/scg/util/*.java

I am trying to compile all the java files in two packages. but I need to have slf4j in my classpath. but somehow I am not able to get it work. Thanks

like image 477
eagertoLearn Avatar asked Feb 12 '14 20:02

eagertoLearn


People also ask

How do I fix class path contains multiple SLF4J bindings?

If you are looking for quick solution for this issue, you need to find out how log4j is present on your path. run mvn dependency:tree and figure out the maven dependency and exclude log4j with snippet below to that dependency in your pom. xml . This should resolve SLF4J: Class Path Contains Multiple SLF4J Bindings.

Does SLF4J depend on log4j?

So essentially, SLF4j does not replace LOG4j; they work together hand in hand. It removes the dependency on LOG4j from your application and make it easy to replace it in future with more capable library without any kind of source code changes.

Is log4j and SLF4J same?

Unlike log4j, SLF4J (Simple Logging Facade for Java) is not an implementation of logging framework, it is an abstraction for all those logging frameworks in Java similar to log4J. Therefore, you cannot compare both.


1 Answers

This dependency is the api:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.5</version>
</dependency>

You need the slf4j-api.jar in your classpath for compiling, not the slf4j-log4j12.jar.

It worked in maven because the binding-lib (slf4j-log4j12) has a dependency on the api and thus maven loads that as well, without you explicitly defining it as a dependency.

like image 165
sheltem Avatar answered Oct 06 '22 02:10

sheltem