This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Log4j Log is not created when jar file run Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Log4j Log is not created when jar file run" Watch "Log4j Log is not created when jar file run" New topic
Author

Log4j Log is not created when jar file run

pradeep gamage
Ranch Hand

Joined: Aug 03, 2009
Posts: 85

I have added log4j into my java project.when i run the project log file is created and i can see log detail in created log file. but when i create jar file and run it log file is not created. Please give me some valuable idea.

Thanks in Advance


Software Engineer(BSC):SCJP 1.5
(Knowledge is power when applied)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Do you get any error messages or other output?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
pradeep gamage
Ranch Hand

Joined: Aug 03, 2009
Posts: 85

I havent get any error. but log is not created.
Mahesh Kedari
Ranch Hand

Joined: Nov 28, 2009
Posts: 111
try running your jar on command line. It will give you the details if there is any initialization error for log4j framework.


Regards,
Mahesh Kedari - Fidus Technologies Ltd.
pradeep gamage
Ranch Hand

Joined: Aug 03, 2009
Posts: 85


when i run on command prompt its give following error. but when i run project with eclipes log is created.

log4j:ERROR Could not read configuration file [log4j.properties].
java.io.FileNotFoundException: log4j.properties (The system cannot find the file specified)

give me idea why is this probleam
Mahesh Kedari
Ranch Hand

Joined: Nov 28, 2009
Posts: 111
Seems like a class path issue. When not configured explicitly, log4j.properties file is picked from default package. Make sure that your log4j.properties file is present at correct location.
For proper location
One simple trick:
Add a file creation code snippet in your project (give only file name without path). Check the location where your new file is getting created. Add your log4j.properties file at that location and try.

pradeep gamage
Ranch Hand

Joined: Aug 03, 2009
Posts: 85

How i can set relative path to log4j.properties file
pradeep gamage
Ranch Hand

Joined: Aug 03, 2009
Posts: 85

This is my Student class that used to test log4j.



This is my log4j.properties file




when i run using eclipes MyLogExample.log file created. then after i created jar file and run using command prompt. but when i run jar file log file is not created.

in console i can see this error.

log4j:ERROR Could not read configuration file [log4j.properties].
java.io.FileNotFoundException: log4j.properties (The system cannot find the file specified)

Give me idea why is this log file is not create with jar file.

Thanks in Advance...
pradeep gamage
Ranch Hand

Joined: Aug 03, 2009
Posts: 85

After i add following code example, log file created when jar file run with command prompt.



How I can give relative path instead of exact path ?
pradeep gamage
Ranch Hand

Joined: Aug 03, 2009
Posts: 85

I have put log4j.properties file in class path. then Load using following code. Now Log4j Logger is working.

Mahesh Kedari
Ranch Hand

Joined: Nov 28, 2009
Posts: 111
In my last post i've already given you a simple trick for placing it at relative location. some more analysis on that will give you a solution.
pradeep gamage
Ranch Hand

Joined: Aug 03, 2009
Posts: 85

Thanks Mahesh. Your idea was very helpful for me... Thanks again....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Log4j Log is not created when jar file run
 
Similar Threads
Log4j
problem of log4j in tomcat 4.1.27
Log4j rolling policy must be declared?
Jar File dependencies
How t o have multiple log files in WSAD and WAS using log4j ?