为什么只有 Log4j 的 Java import 语句起作用,而另一个不起作用?
Why is it that only this Java import statement for Log4j works, but not the other one?
我很好奇 Java 导入是如何工作的。例如,我只是 运行 这个示例 log4j 代码,它没有编译:
import org.apache.log4j.Logger;
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");
}
}
它给出了这个错误:
3 errors found:
File: C:\Users\adel\Desktop\various_topics\new_Java_Code\log4jExample.java [line: 1]
Error: package org.apache.log4j does not exist
File: C:\Users\adel\Desktop\various_topics\new_Java_Code\log4jExample.java [line: 13]
Error: cannot find symbol
symbol: class Logger
location: class log4jExample
File: C:\Users\adel\Desktop\various_topics\new_Java_Code\log4jExample.java [line: 13]
Error: cannot find symbol
symbol: variable Logger
location: class log4jExample
但后来我有另一个 class,它有一个像这样的导入:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Level;
public class HelloWorldLog4J {
而且这个很好用。我很好奇,Java 实际上在哪里可以找到那些导入的库?
谢谢
import
语句是一项功能,它允许您在源代码中使用类型的简单名称(and/or 它们的成员),而不是它们的完全限定名称。例如,隐式
import java.lang.*;
添加到所有 java 编译单元(.java 源文件)允许您编写
String variable = "";
而不是
java.lang.String variable = "";
编译源代码时,编译器需要能够识别您使用的类型,以便判断您是否正确使用它们。典型的 javac
compiler 具有以下 -cp
选项
-cp path
or -classpath path
Specify where to find user class files, and (optionally) annotation processors and source files. This class path overrides the
user class path in the CLASSPATH environment variable. If neither
CLASSPATH, -cp
nor -classpath
is specified, the user class path
consists of the current directory. See Setting the Class Path for more
details.
此选项允许您指定任何编译的 .class
文件的位置(提取的或在库存档中,例如:.jar
)。
当您编译代码时,大概是通过 IDE 或构建工具,实际执行的代码类似于
javac -cp "/some/folder/with/classes:log4j2.jar" com/example/log4jExample.java
换句话说,它指的是包含所有日志记录的 log4j2 库 classes。您遇到编译错误是因为您的 class 路径不包含名为 org.apache.log4j.Logger
的 class,因此编译器无法验证您尝试执行的操作。在您的第二次尝试中,class路径确实包含一个名为 org.apache.logging.log4j.LogManager
(和其他)的 class。
org.apache.log4j.Logger
是 log4j 1 的一部分。它不是 log4j 2 的一部分。
我很好奇 Java 导入是如何工作的。例如,我只是 运行 这个示例 log4j 代码,它没有编译:
import org.apache.log4j.Logger;
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");
}
}
它给出了这个错误:
3 errors found:
File: C:\Users\adel\Desktop\various_topics\new_Java_Code\log4jExample.java [line: 1]
Error: package org.apache.log4j does not exist
File: C:\Users\adel\Desktop\various_topics\new_Java_Code\log4jExample.java [line: 13]
Error: cannot find symbol
symbol: class Logger
location: class log4jExample
File: C:\Users\adel\Desktop\various_topics\new_Java_Code\log4jExample.java [line: 13]
Error: cannot find symbol
symbol: variable Logger
location: class log4jExample
但后来我有另一个 class,它有一个像这样的导入:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.Level;
public class HelloWorldLog4J {
而且这个很好用。我很好奇,Java 实际上在哪里可以找到那些导入的库? 谢谢
import
语句是一项功能,它允许您在源代码中使用类型的简单名称(and/or 它们的成员),而不是它们的完全限定名称。例如,隐式
import java.lang.*;
添加到所有 java 编译单元(.java 源文件)允许您编写
String variable = "";
而不是
java.lang.String variable = "";
编译源代码时,编译器需要能够识别您使用的类型,以便判断您是否正确使用它们。典型的 javac
compiler 具有以下 -cp
选项
-cp path
or-classpath path
Specify where to find user class files, and (optionally) annotation processors and source files. This class path overrides the user class path in the CLASSPATH environment variable. If neither CLASSPATH,
-cp
nor-classpath
is specified, the user class path consists of the current directory. See Setting the Class Path for more details.
此选项允许您指定任何编译的 .class
文件的位置(提取的或在库存档中,例如:.jar
)。
当您编译代码时,大概是通过 IDE 或构建工具,实际执行的代码类似于
javac -cp "/some/folder/with/classes:log4j2.jar" com/example/log4jExample.java
换句话说,它指的是包含所有日志记录的 log4j2 库 classes。您遇到编译错误是因为您的 class 路径不包含名为 org.apache.log4j.Logger
的 class,因此编译器无法验证您尝试执行的操作。在您的第二次尝试中,class路径确实包含一个名为 org.apache.logging.log4j.LogManager
(和其他)的 class。
org.apache.log4j.Logger
是 log4j 1 的一部分。它不是 log4j 2 的一部分。