Thursday, September 30, 2010

Log4j and Netbeans

A number of projects use log4j it was very annoying to get an error message when I started to use it in netbeans. Log4j doesn't run seamlessly when added as a library to a project a little set up was in order. The first part of the solution (I forget where it was suggested so I apologize for the plagiarism) is to use Log4j itself to help find the problem. Just put Log4j into 'debug' mode and it starts working for you telling you intimate details about what is going wrong. The debug message trail from the Log4j library were saying it couldn't find log4j.properties or log4j.xml. These are files that specify the different appenders and the various default settings for Log4j.

For some reason the runtime is not able to locate these files even though they were at the top level of the project directory. It turns out that the log4j.properties and log4j.xml need to be in a directory listed in your class paths in the project properties. That would be obvious if this were a java runtime class but you expect that including the jar file would be enough to get a minimal configuration for running commands. It turns out no. These files need to be placed in a path that is findable by Netbeans project's classpath.

The following link will explain it in greater detail
http://netbeans.org/kb/docs/java/project-setup.html#projects-configuring

To set this under Netbeans go to 'Run>Set Project Configuration>Customize' that will bring up a Dialog box where both Log4j reporting level and the classpath can be altered for use inside of Netbeans. For the class path Choose the Libraries selection on the left of the box. Inside this sequence of menus choose Jar file/Folder. In this case I choose to set the folder as $HOME/NetbeansProjects/MyProject/src.

Netbeans would not let me use the very top level of the project directory as a folder for the classpath. I didn't bother to find out why nor did I investigate whether I really want it in 'src'. It probably needs to go in a directory that will get 'jar'ed up so it will deploy into my command directory seamlessly but that will be addressed when I'm ready to deploy multiple Java commands and string them together under MacOS unix shell environment.