Wednesday, February 24, 2010

Creating log4j (logging) for a EJB\WEB project




If we have a big project with both Web and EJB projects and a EAR project to link them and if we want to implement logging, in this case here is the approval we need to take.

In the EAR project, under META-INF folder, we need to create a folder called "APP-INF" under this folder, we need to place the log4j.jar file.

Once we add the jar file, we need to include a reference to this jar file from Web and EJB project.

Right click on the project "OCEDecisionEJB" and "OCEDecisionWeb", properties, Java build path, Libraries tab, click "Add JARs" button and select the log4j.jar from EAR project.

As we can see in the snapshot, the log4j.jar in the EJB project refers to the EAR project. (OCEDecisionEAR/META-INF/APP-INF).

Once this is done, we need to open the META-INF\MANIFEST.MF file in both EJB and WEB projects. For Web project it should be under WebContent\META-INF\MANIFEST.MF

MANIFEST.MF = This is basically a "Jar Dependency Editor". We edit this file and add
this line.

Class-Path: META-INF/APP-INF/log4j.jar

This means add the log4j jar file in the classpath.

Once this done, we can verify the details by right click on EJB and Web projects, "J2EE Module Dependencies". We can see the dependency defined here.

Creating jar (war) - Command

This sun link will he help us to understand the basics of creating a jar file and the commands involved.


When we open a directory in C drive, say folder name is myProject, and we have look of files in it.

c:>cd myProject;

We can see 2 things. A single dot (.) and a double dot (..) The single dot means its the current directory and double dot means parent directory.

c:\myProject> cd .

With this command, it is pointing to the current directory and nothing will happen.

c:\myProject> cd ..

With this command, it is pointing to the parent directory and it will move one level up and reach its parent directory.

c:\>

With that understanding we can now look at creating a war file.

Web projects are packaged as war while EJB projects are packaged as ear.

Web Project = myProject.war
EJB Project = myProject.ear

c:\myProject\webProject\Webcontent\jar -cvf oce.war .

jar command

c = create
v = verbose (like logger, which prints all the details on the command prompt)
f = File name (Here all the files will packed under the name we give, ex: oce.war)
dot (.) = This dot means, pack everything under this folder.

There is one important thing we should note here. We should pack (creating .war file) things within the "Webcontent" folder rather "WebProject" folder.

CONTACT

p> You can reach me @ My EMail ID

Powered by Blogger