在Log4j 2.0中发现ClassNotFoundException[英] ClassNotFoundException found in Log4j 2.0

本文是小编为大家收集整理的关于在Log4j 2.0中发现ClassNotFoundException的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

im已经为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;
}



 }

和我的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());

}

}

它给出以下例外:

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.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.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$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.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

推荐答案

我刚刚从这里下载了log4j 2.0 .apache.org/log4j/2.x/download.html

我还没有使用过它,但是看起来您可能需要log4j-api-2.0-beta2.jar以及log4j-core-2.0-beta2.jar log4j-core-2.0-beta2.jar.我猜是API JAR是这样您可以编译的,并且核心包含实现.

其他推荐答案

您是否正在使用IDE(例如Eclipse),并且您是否从运行代码中获得了stacktrace(而不是在编译它时)?

?

在这里只是一个疯狂的猜测,但是您可能只是设置构建路径(因此您的项目可以编译还可以),但是您在运行时的类路径没有log4j jars.

在Eclipse中您可以导出一个JAR文件,以便在运行时可用.

其他推荐答案

Venkymca,

您需要导入以下软件包以与Log4J 2.0

一起使用
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

他们的工作正常.

本文地址:https://www.itbaoku.cn/post/1575015.html

问题描述

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:

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:

 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.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.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$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.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

推荐答案

I just downloaded log4j 2.0 from here: 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.

其他推荐答案

Are you using an IDE (e.g. Eclipse) and did you get the stacktrace from running your code (instead of while compiling it)?

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.

In Eclipse you can export a JAR file so that it would be available during runtime.

其他推荐答案

VenkyMCA,

You need to import the below packages to work with log4j 2.0

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

And they will work fine.