gpt4 book ai didi

mysql - 无法实例化 SLF4J LoggerFactory

转载 作者:行者123 更新时间:2023-11-29 04:11:50 27 4
gpt4 key购买 nike

所以,

我正在使用这个示例 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去图书馆。

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

然后我将这两个库添加到项目中并点击运行。

我现在得到这个错误:

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 的统一接口(interface)。

slf4j-log4j12 提供到 log4j API 的转换。由于您不包含 log4j 库,因此会引发错误。不包括 slf4j-log4j12 库就足够了(如果只包括 slf4j-api 库,那么它应该默认为无操作记录器 AFAIK)。

关于mysql - 无法实例化 SLF4J LoggerFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8388078/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com