Fail to import org.slf4j.Logger- how to add the jar to classpath?

20,230

Solution 1

As I had run to very similar issue, I write what my problem was:

I copied a wrong maven dependency from the internet (example). It was only in test scope.. Oh Dear..

<scope>test</scope>

So after remove scope test section started to work as expected. :)

Solution 2

You said you are new to Maven and have added the dependency in POM. Now I am assuming that, you are assuming that, just because you added something in pom.xml, the dependent jars will be available for build.

Maven does not work that way. It points to a common repository from where you have to download the dependent jars (which happens when you do an mvn install)

After you mvn install works fine, you may use this src to setup in your eclipse IDE either by using mvn eclipse:eclipse or using an eclipse maven plugin.

So, after you added the dependency, run following command from where your project is located

mvn clean install;

Your jars should be downloaded from maven to your local directory something like:

C:\Users\user.m2\repository

Share:
20,230
user3735871
Author by

user3735871

Updated on April 25, 2020

Comments

  • user3735871
    user3735871 about 4 years

    I am learning to use Maven and Log4J. In the POM file I've added the below dependencies:

      <dependencies>
         <dependency>
           <groupId>org.slf4j</groupId>
           <artifactId>slf4j-log4j12</artifactId>
           <version>1.7.12</version>
         </dependency>
         <dependency>
           <groupId>log4j</groupId>
           <artifactId>log4j</artifactId>
           <version>1.2.17</version>
         </dependency>   
      </dependencies>
    

    When I tried to import import org.slf4j.Logger in my main class, Eclipse says "The import org.slf4j cannot be resolved". I searched a while for an solution, and it seems that I need to download the distribution file here http://www.slf4j.org/download.html and "add the jar to the classpath"? I am confused: wouldn't Maven download that automatically? How can I "add the jar to the classpath"? I am a real beginner here. Many thanks for your help!