使用Simple Logging Facade for Java时的NoClassDefFoundError,即使使用slf4j-api.jar也是如此

时间:2017-06-12 02:14:29

标签: java database connection slf4j

SLF4J文档指出,如果发生NoClassDefFoundError,应将slf4j-api-1.7.25 jar文件添加到类路径中。我已将此文件添加到我的类路径中,但仍然收到此错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFac
        at SchemaDetailsTest.<clinit>(SchemaDetailsTest.java:9)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 1 more

这是我的类路径:

java -cp "C:\Users\Tiffany\Desktop\baffle-sql-parser-master\library\src\demos\analyzescript\slf4j-1.7.25.jar;C:\Users\Tiffany\Desktop\baffle-sql-parser-master\library\src\demos\analyzescript\slf4j-1.7.25\slf4j-api-1.7.25-sources.jar;C:\Users\Tiffany\Desktop\baffle-sql-parser-master\library\src\demos\analyzescript\slf4j-1.7.25\slf4j-simple-1.7.25.jar;C:\Users\Tiffany\Desktop\baffle-sql-parser-master\library\src\demos\analyzescript\mysql-connector-java-5.1.42-bin.jar;C:\Users\Tiffany\Desktop\baffle-sql-parser-master\library\src\demos\analyzescript" SchemaDetailsTest

以下是一些代码:

 private static Logger log = LoggerFactory
            .getLogger(SchemaDetailsTest.class);
    public static void main(String args[]) throws Exception {
        String databaseName = "myDbName";
        String JDBC_DRIVER = "jdbc:mysql://34.210.136.158:3306/";
        String USER = "servicenowapp";
        String PWD = "test";
        // Setup the connection with the DB
       Class.forName("com.mysql.jdbc.Driver");
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(JDBC_DRIVER, USER, PWD);
        }catch(Exception e){
            e.printStackTrace();
        }

任何建议都将不胜感激!

2 个答案:

答案 0 :(得分:1)

slf4j-api-1.7.25-sources.jar是Java源代码,您需要二进制文件。将其更改为slf4j-api-1.7.25.jar

您也可以稍微简化一下类路径(如果将其配置为环境变量,您会发现它更容易维护)

set "SLF4J_VER=1.7.25"
set "MYSQL_VER=5.1.42"
set "DESKTOP=C:\Users\Tiffany\Desktop"
set "SCRIPT_LIB=%DESKTOP\baffle-sql-parser-master\library\src\demos\analyzescript"
set "SLF4J_LIB=%SCRIPT_LIB%\slf4j-%SLF4J_VER%"
set "CLASSPATH=%SLF4J_LIB%\slf4j-api-%SLF4J_VER%.jar;%SLF4J_LIB%\slf4j-simple-%SLF4J_VER%.jar;%SCRIPT_LIB%\mysql-connector-java-%MYSQL_VER%-bin.jar;%SCRIPT_LIB%"
java SchemaDetailsTest

答案 1 :(得分:0)

错误是您添加了错误的来源 jar slf4j-api-1.7.25-sources.jar,您必须添加 bin jar,例如slf4j-api-1.7.25.jar