Failed to instantiate SLF4J LoggerFactory

14,729

If you include slf4j-log4j12-1.6.4.jar, then you must also include the log4j jar. Slf4j is a logging facade, which means it gives you a uniform interface to multiple other logging APIs.

The slf4j-log4j12 provides a conversion to the log4j API. As you don't include the log4j library, it throws an error. Not including the slf4j-log4j12 library should be enough (if only the slf4j-api library is included, then it should then default to a no-operation logger AFAIK).

Share:
14,729
davidahines
Author by

davidahines

Java Developer at CGI Atlantic.

Updated on June 04, 2022

Comments

  • davidahines
    davidahines almost 2 years

    So,

    I'm working from this example BONECP:

    package javasampleapps;
    
    import java.sql.Connection;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import com.jolbox.bonecp.BoneCP;
    import com.jolbox.bonecp.BoneCPConfig;
    /** A test project demonstrating the use of BoneCP in a JDBC environment.
     * @author wwadge
     */
    public class BoneCPExample {
        /** Start test
         * @param args none expected.
         */
        public static void main(String[] args) {
            BoneCP connectionPool = null;
            Connection connection = null;
            try {
                // load the database driver (make sure this is in your classpath!)
                Class.forName("com.mysql.jdbc.Driver");
            } catch (Exception e) {
                e.printStackTrace();
                return;
            }
            try {
                // setup the connection pool
                BoneCPConfig config = new BoneCPConfig();
                config.setJdbcUrl("jdbc:mysql://domain/db"); 
                config.setUsername("root"); 
                config.setPassword("pass");
                config.setMinConnectionsPerPartition(5);
                config.setMaxConnectionsPerPartition(10);
                config.setPartitionCount(1);
                connectionPool = new BoneCP(config); // setup the connection pool
    
                connection = connectionPool.getConnection(); // fetch a connection
    
                if (connection != null){
                    System.out.println("Connection successful!");
                    Statement stmt = connection.createStatement();
                    ResultSet rs = stmt.executeQuery("SELECT id from batches limit 1"); // do something with the connection.
                    while(rs.next()){
                        System.out.println(rs.getString(1)); // should print out "1"'
                    }
                }
                connectionPool.shutdown(); // shutdown connection pool.
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                if (connection != null) {
                    try {
                        connection.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    

    I added slf4j in my Libraries menu in netbeans, adding D:/Documents%20and%20Settings/DavidH/My%20Documents/NetBeansProjects/jars/slf4j-api-1.6.4.jar and D:/Documents%20and%20Settings/DavidH/My%20Documents/NetBeansProjects/jars/slf4j-log4j12-1.6.4.jar to the library.

    Then I made a Google Guava library and added the jar that they distribute for that to another library.

    I then added both of the libraries to the project and hit run.

    I now get this error:

    Failed to instantiate SLF4J LoggerFactory
    Reported exception:
    java.lang.NoClassDefFoundError: org/apache/log4j/Level
        at org.slf4j.LoggerFactory.bind(LoggerFactory.java:128)
        at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:108)
        at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:279)
        at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:252)
        at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:265)
        at com.jolbox.bonecp.BoneCPConfig.<clinit>(BoneCPConfig.java:60)
        at javasampleapps.BoneCPExample.main(BoneCPExample.java:28)
    Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Level
        at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
        ... 7 more
    Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Level
        at org.slf4j.LoggerFactory.bind(LoggerFactory.java:128)
        at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:108)
        at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:279)
        at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:252)
        at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:265)
        at com.jolbox.bonecp.BoneCPConfig.<clinit>(BoneCPConfig.java:60)
        at javasampleapps.BoneCPExample.main(BoneCPExample.java:28)
    Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Level
        at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
        ... 7 more
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)
    

    What can I do to fix this?