ClassNotFoundException found in Log4j 2.0

im already set the build path for log4j12-api-beta2.jar, but it gives after error, please help me solve this problem
my code should be a 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:

  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()); } } 

he 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 
+4
source share
3 answers

I just downloaded log4j 2.0 from here: http://logging.apache.org/log4j/2.x/download.html

I haven't used it yet, but it looks like you probably need both log4j-api-2.0-beta2.jar log4j-core-2.0-beta2.jar in the classpath. I assume api jar is something you can compile, and the kernel contains an implementation.

+1
source

Are you using an IDE (e.g. Eclipse) and are you trying to run stacktrace from your code (rather than compiling it)?

Just a wild hunch here, but you can just set the build path (so your project will compile in order), but your class path at runtime has no Log4J cans.

In Eclipse, you can export a JAR file so that it is available at run time.

0
source

VenkyMCA,

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

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

And they will work well.

-5
source

Source: https://habr.com/ru/post/1445053/


All Articles