This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Log4j Log is not created when jar file run

 
pradeep gamage
Ranch Hand
Posts: 85
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15205
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you get any error messages or other output?
 
pradeep gamage
Ranch Hand
Posts: 85
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I havent get any error. but log is not created.
 
Mahesh Kedari
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try running your jar on command line. It will give you the details if there is any initialization error for log4j framework.
 
pradeep gamage
Ranch Hand
Posts: 85
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How i can set relative path to log4j.properties file
 
pradeep gamage
Ranch Hand
Posts: 85
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have put log4j.properties file in class path. then Load using following code. Now Log4j Logger is working.

 
Mahesh Kedari
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mahesh. Your idea was very helpful for me... Thanks again....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic