java 在 Log4j 2.0 中发现 ClassNotFoundException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13308812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ClassNotFoundException found in Log4j 2.0
提问by venkyMCA
im already set the build path for log4j12-api-beta2.jar but it gives the
following error please help me to solve this problem
my code is follows
java file:
我已经为 log4j12-api-beta2.jar 设置了构建路径,但它给出了以下错误请帮我解决这个问题
我的代码遵循 java 文件:
package com.sst.log4j;
class Product {
private int productId;
private String productName;
public int getProductId() {
return productId;
}
public void setProductId(int productId) {
this.productId = productId;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public Product(int productId, String productName) {
super();
this.productId = productId;
this.productName = productName;
}
}
and my Main() file is:
我的 Main() 文件是:
package com.sst.log4j;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
public class ProductMain {
/**
* @param args
*/
static Logger log=LogManager.getLogger(Product.class.getName());
public static void main(String[] args) {
// TODO Auto-generated method stub
Product p1=new Product(1,"garlands");
System.out.println(p1.getProductName());
log.error(p1.getProductName());
}
}
it gives the following Exception:
它给出了以下异常:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/logging/
log4j/LogManager
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access0(URLClassLoader.java:71)
at java.net.URLClassLoader.run(URLClassLoader.java:361)
at java.net.URLClassLoader.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at org.apache.log4j.LogManager.getLogger(LogManager.java:38)
at com.sst.log4j.ProductMain.main(ProductMain.java:14)
Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.LogManager
at java.net.URLClassLoader.run(URLClassLoader.java:366)
at java.net.URLClassLoader.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
... 14 more
回答by Upgradingdave
I just downloaded log4j 2.0 from here: http://logging.apache.org/log4j/2.x/download.html
我刚刚从这里下载了 log4j 2.0:http: //logging.apache.org/log4j/2.x/download.html
I haven't used it yet, but looks like you probably need both log4j-api-2.0-beta2.jar
as well as log4j-core-2.0-beta2.jar
on the classpath. I'm guessing the api jar is so you can compile and the core contains the implementation.
我还没有使用它,但看起来你可能需要这两个log4j-api-2.0-beta2.jar
以及log4j-core-2.0-beta2.jar
在类路径上。我猜 api jar 是这样你可以编译并且核心包含实现。
回答by KC Wong
Are you using an IDE (e.g. Eclipse) and did you get the stacktrace from running your code (instead of while compiling it)?
您是否使用 IDE(例如 Eclipse)并且您是否通过运行代码(而不是在编译时)获得了堆栈跟踪?
Just a wild guess here, but you might just be setting build path (so your project will compile okay), but your classpath during runtime doesn't have the Log4J jars.
这里只是一个疯狂的猜测,但您可能只是在设置构建路径(因此您的项目可以正常编译),但是您在运行时的类路径没有 Log4J jar。
In Eclipse you can export a JAR file so that it would be available during runtime.
在 Eclipse 中,您可以导出 JAR 文件,以便它在运行时可用。
回答by Blue_Jean_White_TShirt
VenkyMCA,
VenkyMCA,
You need to import the below packages to work with log4j 2.0
您需要导入以下包才能使用 log4j 2.0
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
And they will work fine.
他们会工作得很好。