[logback-user] External property file

Tony Trinh tony19 at gmail.com
Tue Apr 23 02:12:40 CEST 2013


Hi Suriyanto,

I should've been clearer in my earlier email. You should update your
application jar's manifest (not SLF4J or logback) to specify a default
classpath (using the "Class-Path" entry in META-INF/MANIFEST.MF). This is
usually done as part of your application's build configuration. The
following links (copied from previous email) provide an example in Ant and
Maven.

[5]
http://stackoverflow.com/questions/9532716/adding-the-current-directory-to-a-jar-files-classpath
[6]
http://stackoverflow.com/questions/4126030/executable-jar-wont-find-the-properties-files

For testing purposes, you could add to your classpath from the command line
as shown in the following example:

unix:
    $ java -cp /dir:/dir/myapp.jar MyApp

windows:
    C:\> java -cp C:\dir;C:\dir\myapp.jar MyApp

(assuming "MyApp" is the main class, and "/dir" is the directory that
contains log.properties and myapp.jar)

...but you wouldn't need this if you've added to the classpath from your
application jar's manifest.


-Tony


On Sun, Apr 21, 2013 at 12:23 AM, Suriyanto <suriyanto at gmail.com> wrote:

> Hi Tony,
>
> I believe I found the issue.
>
> I have the jar and the properties file inside dir1. I am calling the jar
> from the parent directory of dir1 using the command below:
>
> java -jar dir1/myapp.jar
>
> I believe this caused logback to try to find the properties file on the
> current directory, which is the parent of dir1. Is there a way for logback
> to be able to constantly loading the properties from the same directory
> that the jar is in?
>
> Thanks,
> Suriyanto
>
>
> On Sat, Apr 20, 2013 at 9:25 AM, Tony Trinh <tony19 at gmail.com> wrote:
>
>> I'm not sure why that quick test (using the "file" attribute, and running
>> the jar from the same directory as log.properties) did not work for you. It
>> worked for me in OSX Mountain Lion and JDK 6. The PropertyAction [1] passes
>> the "file" attribute's value directly to FileInputStream [2], which uses
>> File [3]. The Oracle javadocs should provide more details.
>>
>> It turns out the "resource" attribute should also work in this case (I've
>> confirmed on my machine). This uses ClassLoader.getResource(), whose search
>> order is defined by [4]. You need to include the directory of
>> log.properties in the jar's classpath either by command-line or from the
>> jar's manifest [5][6].
>>
>> [1]
>> https://github.com/qos-ch/logback/blob/master/logback-core/src/main/java/ch/qos/logback/core/joran/action/PropertyAction.java#L72
>> [2] http://docs.oracle.com/javase/6/docs/api/java/io/FileInputStream.html
>> [3] http://docs.oracle.com/javase/6/docs/api/java/io/File.html
>> [4]
>> http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/ClassLoader.html#getResource%28java.lang.String%29
>> [5]
>> http://stackoverflow.com/questions/9532716/adding-the-current-directory-to-a-jar-files-classpath
>>  [6]
>> http://stackoverflow.com/questions/4126030/executable-jar-wont-find-the-properties-files
>>
>>
>> On Sat, Apr 20, 2013 at 12:08 AM, Suriyanto <suriyanto at gmail.com> wrote:
>>
>>> Thanks again, Tony.
>>>
>>> Yes, if I use absolute path, it will work. However I want to use the
>>> relative path so the user can put the jar and properties file anywhere as
>>> long as they are put in the same directory.
>>>
>>> I tried to cd to dir and run from there, but logback still tells me that
>>> it cannot find the properties file. Is there a way for me to know what the
>>> absolute mapping that logback use when I provide a relative path?
>>>
>>> Thanks.
>>>
>>>
>>> On Fri, Apr 19, 2013 at 9:15 PM, Tony Trinh <tony19 at gmail.com> wrote:
>>>
>>>> Ok. The file-lookup is relative to your current working directory
>>>> (unless you use absolute paths). You can confirm by cd'ing into /dir and
>>>> then invoking your jar.
>>>>
>>>> An alternative is to use an absolute path to a designated "data"
>>>> directory (with environment variables). For example:
>>>>
>>>>     <property file="${HOME}/.myapp/log.properties" />
>>>>      <property file="${APPDATA}/.myapp/log.properties" />
>>>>     <property file="${PROGRAMFILES}/MyApp/log.properties" />
>>>>
>>>>
>>>>
>>>>
>>>> On Fri, Apr 19, 2013 at 9:22 PM, Suriyanto <suriyanto at gmail.com> wrote:
>>>>
>>>>> Thanks for answering.
>>>>>
>>>>> I actually wanted to put the file outside the jar so the user can
>>>>> easily modify it for any of their environment. The file will be on the same
>>>>> directory as the jar file.
>>>>>
>>>>> /dir
>>>>>     myapp.jar
>>>>>     log.properties
>>>>>
>>>>> Suriyanto
>>>>>
>>>>>
>>>>> On Fri, Apr 19, 2013 at 7:00 PM, Tony Trinh <tony19 at gmail.com> wrote:
>>>>>
>>>>>> If the file is in your JAR, you should be using the "resource"
>>>>>> attribute instead of "file" (which searches your host filesystem).
>>>>>>
>>>>>>   <property resource="log.properties" />
>>>>>>
>>>>>>
>>>>>> On Fri, Apr 19, 2013 at 8:39 PM, Suriyanto <suriyanto at gmail.com>wrote:
>>>>>>
>>>>>>> Hello,
>>>>>>>
>>>>>>> I am following the instruction in the Logback manual to use an
>>>>>>> external property file.
>>>>>>>
>>>>>>> <property file="log.properties" />
>>>>>>>
>>>>>>> I have log.properties file in the same directory as my executable
>>>>>>> jar file, which are created as an uber jar file from Maven plugin.
>>>>>>>
>>>>>>> However, I keep getting the error: ERROR in
>>>>>>> ch.qos.logback.core.joran.action.PropertyAction - Could not find properties
>>>>>>> file [log.properties].
>>>>>>>
>>>>>>> Am I putting the file in the correct directory?
>>>>>>>
>>>>>>> Thanks,
>>>>>>> Suriyanto
>>>>>>>
>>>>>>> _______________________________________________
>>>>>>> Logback-user mailing list
>>>>>>> Logback-user at qos.ch
>>>>>>> http://mailman.qos.ch/mailman/listinfo/logback-user
>>>>>>>
>>>>>>
>>>>>>
>>>>>> _______________________________________________
>>>>>> Logback-user mailing list
>>>>>> Logback-user at qos.ch
>>>>>> http://mailman.qos.ch/mailman/listinfo/logback-user
>>>>>>
>>>>>
>>>>>
>>>>> _______________________________________________
>>>>> Logback-user mailing list
>>>>> Logback-user at qos.ch
>>>>> http://mailman.qos.ch/mailman/listinfo/logback-user
>>>>>
>>>>
>>>>
>>>> _______________________________________________
>>>> Logback-user mailing list
>>>> Logback-user at qos.ch
>>>> http://mailman.qos.ch/mailman/listinfo/logback-user
>>>>
>>>
>>>
>>> _______________________________________________
>>> Logback-user mailing list
>>> Logback-user at qos.ch
>>> http://mailman.qos.ch/mailman/listinfo/logback-user
>>>
>>
>>
>> _______________________________________________
>> Logback-user mailing list
>> Logback-user at qos.ch
>> http://mailman.qos.ch/mailman/listinfo/logback-user
>>
>
>
> _______________________________________________
> Logback-user mailing list
> Logback-user at qos.ch
> http://mailman.qos.ch/mailman/listinfo/logback-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qos.ch/pipermail/logback-user/attachments/20130422/1f91d364/attachment-0001.html>


More information about the Logback-user mailing list