javanetbeanslog4jclassnotfound

log4j classes not found in Netbeans IDE


I am working through Tim Downey's book "Web Development with Java".

In chapter 4 one of the topics is logging with log4j.

In order to initialise the logger an initialisation servlet is used.

One of the classes needed is, e.g., "FileAppender". Hence, there is an import statement at the top of the file for it:

import org.apache.log4j.FileAppender;

I also used the same import statement. However, netbeans is telling me that this class doesn't exist. (The same is true for any other class from the log4j library.)

Normally, when I create an object of a class I have not yet imported, the netbeans IDE offers me to import the respective class.

So I created an object of type "FileAppender":

FileAppender appender = getAppender(logPath);

... I hoped that the IDE would tell me what import-statement to use in order to import this class.

However, no such luck. Apparently, the IDE does not know about such a class.

So, I think that for some reason the IDE does not know where to look. This again means that I probably did something wrong when setting log4j up.

Here's what I did:

  1. I downloaded log4j from the apache website.
  2. I copied the downloaded file (apache-log4j-2.8.2-bin.tar) in a folder on my desktop.
  3. I used Archive Utility to extract the content of the tar file.
  4. As a result of step 4 I then had a folder called "apache-log4j-2.8.2-bin" in the same folder where the tar file is located.
  5. I went to netbeans IDE and followed the steps in this video to add log4j to netbeans: https://www.youtube.com/watch?v=vqhQY7U9Rlc (I called the new library "log4j")
  6. As a result of step 5, I now have an additional library named "log4j" in my Libraries-Folder

But the IDE doesn't seem to know about any log4j packages or classes.

Did I do anything wrong? (What did I do wrong?)

************************UPDATE***************************************

Here's a screenshot of the netbeans library folder after I added log4j:

enter image description here

Here's a picture of the IDE telling me it doesn't know about log4j's package (What you see is the upper part of the file "InitLog4j.java", which again is inside the package named "various". Package "various" is inside the folder named "Source Packages"):

enter image description here

***********************UPDATE2***************************************

The problem exists independent of whether I include log4j as library or as JAR/FOLDER.

After adding log4j as Jar/Folder:

enter image description here

... my netbeans folder structure looks as follows (log4j is in the folder whose path starts with "/Users"):

enter image description here

However, the problem remains the same. Netbeans doesn't know about log4j packages.

**********************UPDATE3***********************************

I found the FileAppender class among the many files inside the log4j folder:

enter image description here

So, the class "FileAppender" exists. Hence, when I create an object of type "FileAppender", the Idea should offer to import the class. However, this does not happen (in the screenshot above you can see that the class "FileAppender" exists (see left side), you can also see that the IDE does not recognise the class (see code on the right side)).

*************************UPDATE4***********************************

Instead of adding the whole folder, I now only added the jar files inside the folder. The netbeans folder structure now looks as follows:

enter image description here

However, the IDE still does not seem to recognise the log4j packages (e.g. for the FileAppender object no import statement is suggested).


Solution

  • As per your screenshot. It seems like you have added this jar as library. You need to add the jar using Add Jar/Folder.. option not the Add Library..

    enter image description here

    It will prompt window to navigate and select your jar.

    enter image description here

    Once, you add them, then it should look like

    enter image description here

    You can expand the jar from here and see, if required class exists inside the jar or not.

    ==Update==

    You need to select only *.jar file not the complete folder. As shown below.

    enter image description here