简体   繁体   English

log4j软件包的导入错误

[英]Import error of log4j package

I am trying to compile the following very simple Java program which will be expanded to use the log4j package: 我正在尝试编译以下非常简单的Java程序,将其扩展为使用log4j包:

import org.apache.log4j.Logger;

public class App {

    public static void main(String[] args) {
        System.out.println("Howdy");
    }
}

I downloaded the binary log4j package from Apache website, untared the file, set the following exports: 我从Apache网站下载了二进制log4j软件包,解压缩了文件,并设置了以下导出:

$ export CLASSPATH=$CLASSPATH:/home/ahmed/temp/log4j/apache-log4j-2.11.0-bin/log4j-core-2.11.0.jar 
$ export PATH=$PATH:/home/ahmed/temp/log4j/apache-log4j-2.11.0-bin

I am getting the following error when I compile App.java: 编译App.java时出现以下错误:

$ javac App.java 
App.java:5: error: package org.apache.log4j does not exist
import org.apache.log4j.Logger;
                       ^
1 error

I feel like I am missing something basic about java packaging/import concepts. 我觉得我缺少有关Java打包/导入概念的基本知识。

Thank you, Ahmed. 谢谢,艾哈迈德。

The Logger interface is distributed in a different jar, you'll need to get the log4j-api-2.11.0.jar file, and include that jar in your classpath, too. Logger接口分布在另一个jar中,您将需要获取log4j-api-2.11.0.jar文件,并将该jar也包含在类路径中。

Also, check the javadoc for log4j version 2.11 (eg. here https://logging.apache.org/log4j/2.0/log4j-api/apidocs/index.html ), Logger is in a different package, org.apache.logging.log4j.Logger . 此外,检查的log4j版本2.11的Javadoc(如这里https://logging.apache.org/log4j/2.0/log4j-api/apidocs/index.html ), Logger是在不同的包, org.apache.logging.log4j.Logger

(of course using an IDE+Maven or Gradle would let it much easier to sort out this type of issues). (当然,使用IDE + Maven或Gradle可以更轻松地解决此类问题)。

include the jar file to your project! 将jar文件包含到您的项目中! or set environment before starting new project! 或在开始新项目之前设置环境! and sometimes after you set environment first on your pc, you should test in dos by cmd, some environment settings need to reboot your pc.no needed on Mac 有时在您首先在PC上设置环境后,您应该通过cmd在dos中进行测试,某些环境设置需要重新启动PC。Mac上不需要

classpath add ;.; classpath添加;。; . means you can use them in any path on your pc 意味着您可以在PC上的任何路径中使用它们

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM