Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get log4J to work - I'm getting "package org.apache.log4j does not exist"

i know this may be a newbie qestion, but I'm having issues with setting up Log4J:

I want to run a log4j demo, and here's my code:

import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;

public class HelloLOG4j {

  private static final Logger logger = Logger.getLogger(Hello.class);

  public static  void main(String argv[]) {
 BasicConfigurator.configure();
 logger.debug("Hello world.");
 logger.info("What a beatiful day.");
  }
}

I set my Classpath: C:\Users\Adel\Downloads\apache-log4j-1.2.17\log4j-1.2.17.jar in both System and User var's

enter image description here

But when I run my program I still get

errors found:
File: C:\Users\Adel\Desktop\various_topics\JavaProjects\HelloLOG4j.java  [line: 2]
Error: package org.apache.log4j does not exist

I know that I set classpath right - if I run cmd line:

C:\Program Files\Java\jdk1.6.0_20>print %LOG4J_HOME%
C:\Users\Adel\Downloads\apache-log4j-1.2.17\log4j-1.2.17.jar is currently bein
g printed
like image 240
Caffeinated Avatar asked Feb 16 '13 18:02

Caffeinated


People also ask

Which package contains log4j?

apache. log4j. The main log4j package.


1 Answers

You need to add log4j home to the classpath as the JVM needs the path to the log4j classes

if on windows, you can use

set classpath=%classpath%;%LOG4J_HOME%

On linux/ ubuntu (much better than windows for development & servers)

export classpath=$classpath:$LOG4J_HOME

then run your app after adding other paths to classpath like

set classpath=%classpath%;c:\users\adel\....

You do not need to add log4JHOME again - as %classpath%; will add to the current classpath.

LOG4J_HOME is not known to Java. It is just used by log4j in case of auto config/default config.

On a side note try using the new log4j2 !

like image 103
tgkprog Avatar answered Oct 10 '22 09:10

tgkprog