-1

Using Intellij IDEA (version 2017.2.1) I have a Java/Maven project in which I want to include slf4j with the slf4j-binding.

I know that StackOverflow as an abundance of questions about slf4j and its missing binding, but most refer to Eclipse. My problem however occurs under Intellij.

In the pom.xml I list under <dependencies>:

<dependency>
     <groupId>org.slf4j</groupId>
     <artifactId>slf4j-api</artifactId>
     <version>1.7.25</version>
 </dependency>
 <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
 <dependency>
     <groupId>org.slf4j</groupId>
     <artifactId>slf4j-simple</artifactId>
     <version>1.7.25</version>
 </dependency>

Now when I let Intellij build an artifact JAR and I then run the JAR from the command line I get the (dreaded) error:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".

Upon inspection the JARdoes indeed not contain slf4j-simple classes (slf4j classes are present though). How can I fix this and instruct Intellij to incorporate slf4j-simple as listed ion the pom.xml?

For completeness, here full project pom.xml Intellij is using:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>au.gov.acic.travelalert</groupId>
    <artifactId>extract-data</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>


    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.25</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-collections4 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-collections4</artifactId>
            <version>4.1</version>
        </dependency>        <dependency>
            <!-- jsoup HTML parser library @ https://jsoup.org/ -->
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.10.3</version>
        </dependency>
    </dependencies>

</project>

Final note: At the beginning when I set up the project logging didn't even work inside the IDE, despite the correct entries in the pom.xml. But somehow, after additionally adding (and subsequent removing) of a dependency entry for slf4j-log4j12, the IDE picks the slf4j-simple up, but still does not bundle it when writing out the artefact JAR...

halloleo
  • 9,216
  • 13
  • 64
  • 122
  • What maven command are you using? Do you have other dependencies in your project and slf4j is the only one not ending up in your jar file? – Michael Aug 11 '17 at 05:04
  • IDE is irrelevant if you are building the `.jar` with `maven`. You should post all the relevant parts of your `.pom` because something is not including it when you do `mvn package` –  Aug 11 '17 at 05:42
  • @Michael I'm not issuing a maven command myself - I let Intellij do this using the *artefact -> build* menu. – halloleo Aug 11 '17 at 05:43
  • @JarrodRoberson I have added the full `pom.xml`. BTW, the question is not a maven-command-line question; it is rather a question *about Intellij and how maven is used in there*, so it is not a duplicate to the other questions. – halloleo Aug 11 '17 at 06:02
  • Idea executes the exact same commands on the command line when you use it from the gui. It is irrelevant if Idea is invoking the commands or you are they are the same commands. –  Aug 11 '17 at 12:58
  • @JarrodRoberson But I don't know which maven command Intellij invokes! Particularly not for its JAR building mechanism PLUS the strange thing is it does work when I run my class in Intellij - so the IDE finds slf4j-simple through the pom.xml, it just doesn't include it in the JAR ... Therefore I would appreciate if you reopen the question, so that Intellij experts can answer how to setup the IDE . Thanks. :-) – halloleo Aug 12 '17 at 02:08
  • The Maven documenation is very well written and easily accessible, as is the IntelliJ documenation. I suggest you read it instead of asking us to read it for you and tell you what it says. –  Aug 12 '17 at 14:10

3 Answers3

0

execute mvn dependency:tree and check:

  • slf4j-api and slf4j-simple must be present there.
  • No other slf4j binding library shoud be present
  • slf4j bridges can be present (those that end with "-slf4j" suffix on library name; for example, log4j-over-slf4j).
  • ensure all slf4j library versions match
  • ensure no duplicate slf4j libraries with different version.

If all of this is ok, no reason for slf4j-api present on artifact but no slf4j-simple. What are you doing to package jar?

albert_nil
  • 1,648
  • 7
  • 9
  • I'm not issuing a maven command myself - I let Intellij do this using the artefact -> build menu. I will try to find out how I can issue this in Intellij - or do you know? – halloleo Aug 11 '17 at 05:57
  • why the negative? in any case, yeah i know you are using intellij. what's the difference? you can execute any maven goal anyway: https://stackoverflow.com/questions/27246065/how-to-execute-mvn-command-using-intellij-idea – albert_nil Aug 12 '17 at 15:10
0

The problem is that IntelliJ's XML configuration for the JAR artifact does not contain the slf4j-simple library.

This explains that IntelliJ finds the lib ray (downloaded according by maven according to the `POM/XML~) and uses it for internal execution of the project, but it does not pack it into the JAR.

The solution is to add the libarary to the XML config, either

  • manually, by editing the XML file. (You find it under [projectroot].idea/artifacts/[projectname_jar.xml)

or

  • via IntelliJ's GUI, by opening the Artifacts dialog (File --> Project structure --> Artifacts) and then adding the Libra to the Output Layout list.
halloleo
  • 9,216
  • 13
  • 64
  • 122
-1

you can try this solution; go to home directory and delete the .m2 directory(hidden) the update the maven project and try to rebuild. and also check your eclipse version if you are using (Indigo, Juno and Kepler ) version of eclipse the upgrade with latest version or stil if you want to work in it then you can refer below like; this stackoverflow link having most sutable answer on this issue:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". error Question by Konstantinos Margaritis and answer by many java experts.

Anshul Sharma
  • 3,432
  • 1
  • 12
  • 17