无法实例化SLF4J LoggerFactory [英] Failed to instantiate SLF4J LoggerFactory

查看:399
本文介绍了无法实例化SLF4J LoggerFactory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以

我正在使用此示例 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();
                }
            }
        }
    }
}

我在netbeans的库"菜单中添加了slf4j, D:/Documents%20and%20Settings/DavidH/My%20Documents/NetBeansProjects/jars/slf4j-api-1.6.4.jar 和 D:/Documents%20and%20Settings/DavidH/My%20Documents/NetBeansProjects/jars/slf4j-log4j12-1.6.4.jar 到图书馆.

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.

然后,我制作了一个Google Guava库,并将他们为此分发的jar添加到另一个库中.

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.

我现在收到此错误:

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)

该如何解决?

推荐答案

如果包含slf4j-log4j12-1.6.4.jar,则还必须包含log4j jar. Slf4j是一个日志记录外观,这意味着它为您提供了到多个其他日志记录API的统一接口.

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.

slf4j-log4j12提供了到log4j API的转换.由于您不包括log4j库,因此会引发错误.不包括slf4j-log4j12库就足够了(如果仅包括slf4j-api库,则应将其默认为无操作记录器AFAIK).

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).

这篇关于无法实例化SLF4J LoggerFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆