运行Log4J Hello World示例时遇到问题,导致"包不存在#34;错误

时间:2015-04-15 22:57:05

标签: java logging log4j

我在运行Log4J示例时遇到问题。我不确定这是配置问题还是编码问题。这是我的示例代码:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Level;

public class HelloWorldLog4J {

    private static final Logger logger = LogManager.getLogger("HelloWorld");


    public static void main(String[] args) {

        System.out.println("pre_loggerDOTinfo");
        System.out.println();
        //logger.setLevel(Level.FATAL);   //used to be.. 

        logger.info("Hello, this is an INFO message");
        logger.warn("Hello, this is an WARN  message");
        logger.fatal("Hello, this is an FATAL  message");
        logger.fatal("Hello, this is an FATAL  message ALSO, #2");
        logger.debug("Hello, this is an FATAL  message");


        logger.info("Hello, this is an INFO message");
        System.out.println();
        System.out.println("post_loggerDOTinfo");

    }



}

但是当我运行它时,我收到以下错误:

C:\Users\Adel\JavaCoding>javac HelloWorldLog4J.java
HelloWorldLog4J.java:1: error: package org.apache.logging.log4j does not exist
import org.apache.logging.log4j.LogManager;
                               ^
HelloWorldLog4J.java:2: error: package org.apache.logging.log4j does not exist
import org.apache.logging.log4j.Logger;
                               ^
HelloWorldLog4J.java:3: error: package org.apache.logging.log4j does not exist
import org.apache.logging.log4j.Level;
                               ^
HelloWorldLog4J.java:7: error: cannot find symbol
    private static final Logger logger = LogManager.getLogger("HelloWorld");
                         ^
  symbol:   class Logger
  location: class HelloWorldLog4J
HelloWorldLog4J.java:7: error: cannot find symbol
    private static final Logger logger = LogManager.getLogger("HelloWorld");
                                         ^
  symbol:   variable LogManager
  location: class HelloWorldLog4J

我相信我的Classpath设置正确,我设置了3个变量:

  

LOG4J_HOME - %log4j-api%;%log4j-core%

     

log4j-api -   C:\ apache_log4j_beta5 \ Apache的log4j的-2.0-β5-BIN \ log4j的-API-2.0-β5-sources.jar

     

log4j-core -   C:\ apache_log4j_beta5 \ Apache的log4j的-2.0-β5-BIN \ log4j的核-2.0-β5-sources.jar

这是我的目录结构(full-image link):

enter image description here

我想知道它是否可能是log4j的错误版本?我的版本就是这个:apache-log4j-2.0-beta5-bin和I downloaded it from this link

任何提示赞赏,谢谢

1 个答案:

答案 0 :(得分:1)

在运行javac之前,需要将log4j jar添加到CLASSPATH变量中。或者,您可以使用-cp选项在那里指定路径。

javac -cp <path> HelloWorldLog4J.java

其中path是log4j jar的绝对路径,每个路径用冒号字符(:)分隔。您还应该在使用cp选项时包含当前目录,这由句点字符(。)

表示

编辑:你想引用包含log4j的类文件的jar文件,而不是java文件,例如: log4j-core-2.0-beta5.jar,而不是log4j-core-2.0-beta5-sources.jar。