如何解决此Log4J导入错误(与类路径有关)? [英] How do I resolve this Log4J import error (relating to Classpath too)?
问题描述
当我运行下面的简单log4J示例,我得到一个错误:
import org.apache.logging.log4j。核心。*;
import java.io. *;
import java.sql.SQLException;
import java.util。*;
public class log4jExample {
/ *获取要打印的实际类名* /
static Logger log = Logger.getLogger(
log4jExample.class.getName ));
public static void main(String [] args)
throws IOException,SQLException {
log.debug(Hello this is a debug message);
log.info(Hello this is a info message);
}
}
,错误内容如下:
错误:package org.apache.logging.log4j.core不存在
错误:找不到符号
symbol:class Logger
location:class log4jExample
文件:C:\Users\adel\Desktop\various_topics\JavaProjects\log4jExample.java [line:10]
错误:找不到符号
symbol:variable Logger
location:class log4jExample
log4J到类路径,如下所示:
我提取了 jar
文件 log4j-core-2.0 -beta4.jar
:
我不知道发生了什么 - import语句如何工作?即在线示例告诉我说:
import org.apache.log4j.Logger;
但是如果我的目录结构是这样:
\apache-log4j-2.0-beta4-bin\org\apache\logging\log4j\core\Logger.java
我必须说:
import org.apache.logging.log4j.core.Logger; 解决方案 / div> 我认为您的类路径中需要 API 和核心 jar。按照这些说明进行构建和安装以及这些。在配置页面上,您要查询的导入行是 import org.apache.logging.log4j.Logger;
,因此 Logger < 需要两个jar(而不是 Log4j 1) .x ),因为:
API分离
Log4j的API与应用程序开发人员明确的实现是分开的,他们可以使用哪些类和方法,同时确保向前兼容性。这允许Log4j团队以一种兼容的方式安全地改进实现。
When I run the following simple log4J example, i get an error:
import org.apache.logging.log4j.core.*;
import java.io.*;
import java.sql.SQLException;
import java.util.*;
public class log4jExample{
/* Get actual class name to be printed on */
static Logger log = Logger.getLogger(
log4jExample.class.getName());
public static void main(String[] args)
throws IOException,SQLException{
log.debug("Hello this is an debug message");
log.info("Hello this is an info message");
}
}
and the error reads:
Error: package org.apache.logging.log4j.core does not exist
Error: cannot find symbol
symbol: class Logger
location: class log4jExample
File: C:\Users\adel\Desktop\various_topics\JavaProjects\log4jExample.java [line: 10]
Error: cannot find symbol
symbol: variable Logger
location: class log4jExample
So I believe I added log4J to the classpath correctly, as shown below:
And I extracted the jar
file log4j-core-2.0-beta4.jar
from the apache directory as so:
I'm not sure what's happening - how does the import statement work? i.e the example online tells me to say:
import org.apache.log4j.Logger;
but what if my directory structure is like this:
\apache-log4j-2.0-beta4-bin\org\apache\logging\log4j\core\Logger.java
Would I have to say:
import org.apache.logging.log4j.core.Logger;
instead?
解决方案 I think you need both the API and the Core jars in your classpath. Follow these instructions for build and install and these for configuration. On the configuration page, the import line you're asking about is import org.apache.logging.log4j.Logger;
, so the Logger
class will be pulled from the API jar.
Log4j 2 needs two jars (as opposed to Log4j 1.x which needed one) because of the:
API Separation
The API for Log4j is separate from the implementation making it clear for application developers which classes and methods they can use while ensuring forward compatibility. This allows the Log4j team to improve the implementation safely and in a compatible manner.
这篇关于如何解决此Log4J导入错误(与类路径有关)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文