Should log4.properties be on the classpath?
Asked Answered
S

6

12

I'm having some problems putting my log4j.properties file on classpath. I can use it when I'm developing (Eclipse Indigo) but, when I export my app as a JAR, I can't.

I've made by hand a MANIFEST.MF file for the exported JAR:

Manifest-Version: 1.0
Main-Class: main.Program
Class-Path: lib/log4j.properties lib/log4j-1.2.15.jar

And then with put the JAR on this file organization:

folder
  |-------- app.jar
  |-------- lib
             |--------- log4j.properties
             |--------- log4j-1.2.15.jar

When I try to run app.jar, they find log4j.jar but not log4j.properties:

log4j:WARN No appenders could be found for logger (main.Program).
log4j:WARN Please initialize the log4j system properly.

My log4j.properties file it's like this:

log4j.rootLogger=INFO, stdout, file

PATTERN=[%d] [%p] [%c{1}]: %m%n

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=${PATTERN}

log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.DatePattern='.'yyyy-MM-dd
log4j.appender.file.File=${logger_file_path}
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=${PATTERN}
Sinistrodextral answered 17/1, 2012 at 15:47 Comment(0)
T
11

Do not put the log4j.properties itself in the classpath, but the directory that contains that file.

Class-Path: lib lib/log4j-1.2.15.jar
Trichosis answered 17/1, 2012 at 15:53 Comment(2)
I've already corrected changing the log4j.properties into the "folder" (where the JAR exists) and change the class-path to ". lib/log4j-1.2.15.jar". Before that I've tried several folder path to "lib" (e.g. /lib/, lib/, ./lib/) and none of them worked.Sinistrodextral
An old thread, but just want to add that this approach really works. Another good thing about this approach is that a user can actually change the log level if they want to because the config file is outside any jar.Holstein
N
18

one more way to do this: -Dlog4j.configuration=file:"./log4j.properties"

Nothingness answered 22/11, 2013 at 9:20 Comment(1)
For log4j version 2 use: -Dlog4j.configurationFile=file:"YourPathHERE/log4j2.xml"Pigfish
C
12

There are three ways l know.

  1. Add log4j.properties to app.jar
  2. Put the log4j.properties to the "folder" (where the JAR exists) and change the class-path to .lib/log4j-1.2.15.jar.
  3. Put the log4j.properties to the folder named "conf" for example, and change the class-path to ./conf/.

I have tried, it works.

Cuprite answered 25/10, 2012 at 6:41 Comment(1)
Thank you very much. My English is very poor!Jennefer
T
11

Do not put the log4j.properties itself in the classpath, but the directory that contains that file.

Class-Path: lib lib/log4j-1.2.15.jar
Trichosis answered 17/1, 2012 at 15:53 Comment(2)
I've already corrected changing the log4j.properties into the "folder" (where the JAR exists) and change the class-path to ". lib/log4j-1.2.15.jar". Before that I've tried several folder path to "lib" (e.g. /lib/, lib/, ./lib/) and none of them worked.Sinistrodextral
An old thread, but just want to add that this approach really works. Another good thing about this approach is that a user can actually change the log level if they want to because the config file is outside any jar.Holstein
H
1

Add log4j.properties to app.jar.

Haggis answered 17/1, 2012 at 16:9 Comment(1)
For now I prefer to have it externallySinistrodextral
N
0

I tried the answer of Jesper, and at first it didn't work. Then I tried with

Class-Path: lib/ lib/log4j-1.2.15.jar

And it worked after that.

Nolannolana answered 24/2, 2016 at 7:43 Comment(0)
N
-1

try to execute:

javar -jar -Dlog4j.configuration=file:"./log4j.properties" app.jar

Newmown answered 23/2, 2015 at 18:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.