gpt4 book ai didi

java - 如何解决此 Log4J 导入错误(也与类路径有关)?

转载 作者:搜寻专家 更新时间:2023-10-31 08:27:12 26 4
gpt4 key购买 nike

当我运行以下简单的 log4J 示例时,出现错误:

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");
}
}

错误信息如下:

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

所以我相信我正确地将 log4J 添加到类路径中,如下所示:

enter image description here

然后我从 apache 目录中提取了 jar 文件 log4j-core-2.0-beta4.jar:

enter image description here

我不确定发生了什么 - 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;

代替?

最佳答案

我认为您的类路径中同时需要 APICore jar。关注these构建和安装说明以及 these用于配置。在配置页面上,您询问的导入行是 import org.apache.logging.log4j.Logger;,因此 Logger 类将从 API 中提取 jar 。

Log4j 2 需要两个 jar(相对于 Log4j 1.x 需要一个)因为:

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.

关于java - 如何解决此 Log4J 导入错误(也与类路径有关)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16270618/

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