Java:log4j初始化错误 [英] Java: log4j initialization error

查看:430
本文介绍了Java:log4j初始化错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用log4j软件包的新手,我没有看到错误:
这是一个非常简单直接的代码示例:

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:



org.apache.logging.log4j.LogManager中线程mainjava.lang.NullPointerException中的异常.getLogger(LogManager.java:129)at
my.package.logging.TestLogger.main(TestLogger.java:15)

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 ...

你能帮忙吗?

推荐答案

你很可能错过了一个依赖项。如果您只在类路径中包含 log4j-api-2.0-beta1.jar 文件,则这很容易重现。

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.

如果你下载了二进制分发,还包括 log4j-core-2.0-beta1.jar 类路径。如果您使用Maven,请将其放入 pom.xml

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:

  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.0-beta1</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.0-beta1</version>
  </dependency>

这篇关于Java:log4j初始化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆