java main log4j,Java:log4j初始化错误

在尝试运行包含Log4j的日志代码时,遇到了NullPointerException。问题出在缺少必要的依赖。解决方案是确保在类路径中同时包含log4j-api-2.0-beta1.jar和log4j-core-2.0-beta1.jar。如果使用Maven,需要在pom.xml文件中添加这两个依赖项。
摘要由CSDN通过智能技术生成

I'm new in using the log4j package and I don't see the error:

This is a very simple and straightforward code sample:

import org.apache.logging.log4j.LogManager;

import org.apache.logging.log4j.Logger;

public class TestLogger {

private static Logger logger;

public static void main(String[] args) {

logger = LogManager.getLogger(TestLogger.class);

logger.info("Hello");

}

}

When I try to compile I get this error:

Exception in thread "main" java.lang.NullPointerException at

org.apache.logging.log4j.LogManager.getLogger(LogManager.java:129) at

my.package.logging.TestLogger.main(TestLogger.java:15)

I am wondering what on earth it's all supposed to mean ...

Could you help here?

解决方案

You're most probably missing one of the dependencies. This is easy reproducible if you only include in your classpath the log4j-api-2.0-beta1.jar file.

If you downloaded the binary distribution, include also the log4j-core-2.0-beta1.jar in your classpath. If your using Maven, put this in your pom.xml:

org.apache.logging.log4j

log4j-api

2.0-beta1

org.apache.logging.log4j

log4j-core

2.0-beta1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值