8 Replies Latest reply: Apr 20, 2011 10:32 AM by TPD-Opitz RSS

    access log4j.properties outside source folder

    833473
      Hi,

      I have developed a standalone java application, for logging the application i have created a log4j.properties in the source folder.
      it is woring fine and the logs are created as specified in the properties file.

      Issue:
      i have created a jar which contains the complete source code and log4j.properties file.

      if i try to create a jar without log4j.properties since the properties file will change by customer frequantly, i need to keep the log4j.properties outside the source folder.

      but my jar is unable to access the log4j.properties when i try to run the appplication.

      Question:

      how do we access the log4j.properties from outside source folder?


      thanks,
      J R
        • 1. Re: access log4j.properties outside source folder
          Kayaman
          user13836688 wrote:
          i have created a jar which contains the complete source code
          Does it also contain the classfiles? How does the source code matter here?
          • 2. Re: access log4j.properties outside source folder
            833473
            yes, it contains all classes and log4.properties file.

            I want to kepp away log4.properties from source folder which is able to access my java classes for logging
            • 3. Re: access log4j.properties outside source folder
              gimbal2
              It is not related to the source folder. The log4j.properties file has to be in the root of the classpath for log4j to find it. That means it could easily be in a completely different jar; as long as that jar ends up in the classpath of the application.

              Another option is to use log4j's programmatic interface to configuration. The quickstart guide shows different ways of configuring log4j from the code:

              http://logging.apache.org/log4j/1.2/manual.html
              • 4. Re: access log4j.properties outside source folder
                TPD-Opitz
                In addition what gimbal2 sad: Do you create a MANIFEST.MF file in your jar?
                If so you should add (or extend) the ClassPath entry to include the current directory ( *.* ) so you can have the log4j.properties file in the folder where you call your jar from:
                C:\Temp> dir /s /b
                C:\Temp\log4j.properties
                C:\Temp\jars\myApp.jar
                C:\Temp> java -jar jars\myApp.jar
                bye
                TPD
                • 5. Re: access log4j.properties outside source folder
                  gimbal2
                  T.PD wrote:
                  In addition what gimbal2 sad: Do you create a MANIFEST.MF file in your jar?
                  If so you should add (or extend) the ClassPath entry to include the current directory ( *.* ) so you can have the log4j.properties file in the folder where you call your jar from:
                  C:\Temp> dir /s /b
                  C:\Temp\log4j.properties
                  C:\Temp\jars\myApp.jar
                  C:\Temp> java -jar jars\myApp.jar
                  bye
                  TPD
                  This is very dangerous. It makes the path not relative to where the jar is, but to where you invoke the java command.
                  • 6. Re: access log4j.properties outside source folder
                    TPD-Opitz
                    gimbal2 wrote:
                    T.PD wrote:
                    In addition what gimbal2 sad: Do you create a MANIFEST.MF file in your jar?
                    If so you should add (or extend) the ClassPath entry to include the current directory ( *.* ) so you can have the log4j.properties file in the folder where you call your jar from:[...]
                    This is very dangerous. It makes the path not relative to where the jar is, but to where you invoke the java command.
                    The OP's request is to have the (log4j) properties file outside of the jar maybe for easier editing.
                    Adding any known folder in the file system to the class path is the only way I know to achieve this (Do you know better?). Folliwing this you could try to guess paths to add hoping that they will exist on all Systems you will use and place the properties file(s) there. On the other hane the current working dir the java command is invoked in is perdictable. Usually it's the directory the jar itself is located...

                    I agree that this solution can be dangerous and opens the possibility to access classes located in a valid package structure below current working dir . But if you add the ' *.* ' as the last entry in your classpath at least the classes from all your other jars are accessed first...

                    bye
                    TPD
                    • 7. Re: access log4j.properties outside source folder
                      gimbal2
                      T.PD wrote:
                      On the other hane the current working dir the java command is invoked in is perdictable. Usually it's the directory the jar itself is located...
                      You already contradict yourself in this very line. Usually is very much not predictable.

                      I believe the original problem was not that the file should be in some arbitrary directory outside of the application jar; the problem is that the log4j configuration needs to be custom tailored for specific customers. Making the log4j file part of the main application source tree in such cases will not work.

                      Perhaps the solution is more in the packaging phase. It should be possible to build the application such that through some control parameter, a customer specific log4j file ends up in the generated jar file. If I put the problem description in that way, the first thing that comes to mind is to use an ANT build script.


                      EDIT: uh, I guess the OP already went that way judging by this other thread :/

                      Error while creating jar from source folder
                      • 8. Re: access log4j.properties outside source folder
                        TPD-Opitz
                        gimbal2 wrote:
                        T.PD wrote:
                        On the other hane the current working dir the java command is invoked in is perdictable. Usually it's the directory the jar itself is located...
                        You already contradict yourself in this very line. Usually is very much not predictable.
                        I see only two possibilities:
                        1. The jar is invoked by either double click in the file explorer or choosing a strat menu icon. What you you thing the current working dir will be? Isn't that sort of predictable?
                        2. A user calls the jar at the command line as I showed in my first post. This will be a rare case most likely done by the developer himself. And he should know where to place the property file.

                        At least I hope you will agree that (unlike any other directory) the current working dir will surely exist on any system.
                        I believe the original problem was not that the file should be in some arbitrary directory outside of the application jar; the problem is that the log4j configuration needs to be custom tailored for specific customers.
                        Only the OP knows...
                        But in that case I of cause would go with cusomer specific properties.jar 's too.

                        bye
                        TPD