java中log4j导入,如何解决此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:

xYAVb.png

And I extracted the jar file log4j-core-2.0-beta4.jar from the apache directory as so:

2GRU0.png

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值