Firstly, not sure if this is the correct sub-forum (or even correct forum) for this question, so sorry to any mod/admin if it needs moved.
OK, background on my situation. I'm writing an application and trying to use SLF4J 1.5.8 (with Log4J 1.2) for application logging. I am packaging my application (including all appropriate libraries) into a Fat Jar file and running it from the command line with the standard "java -jar <filename>". Now, that goes perfectly well if I include the "log4j.properties" file for Log4J in the jar file, but what I'd like to do is to keep the properties file outside of the jar file (so I can edit it without having to rebuild the jar) and also have several differently named configuration files which I can load when I initialise my application (the filename for the configuration I want to load being kept in a configuration file I can edit). This is not going so well. What I have tried to do is to user the System.setProperty() method to force Log4J to use the specific class (in the main method of my application before the logger is used) as such:
However, this doesn't appear to work because when I run my application I get the following warning:
log4j:WARN No appenders could be found for logger (examplepackage.Example).
log4j:WARN Please initialize the log4j system properly.
Can I accomplish what I want to, and if so how?
Secondly I have another problem related to the configuration of the properties file itself. I want to specify the file name for the log file with a relative path (e.g. just put the filename in there and for it to be written in the same directory where my jar file is being executed from), but everything I've seen in examples etc has shown that you need to specify the full path in the properties file. This isn't hugely convenient if you move the application for whatever reason. Is there a way to just specify a the log filename and have the log written in the folder the jar is executing from?
Hopefully you can help me out here before I pull all my hair out!
Re: SLF4J - Pulling Hair Out Trying To Configure
Nov 2, 2009 2:12 PM
(reply 2
of 38) (In reply to
#1 )
I haven't set the class path but I shouldn't need to as all of the libraries are included inside the Fat Jar file. I think if there was a problem locating any of the classes in SLF4J/Log4J I would get a no class definition exception and the application would fall over into a heap rather than running but giving a warning about the configuration. The application functions perfectly well without the logging, I just want it there for obvious reasons. Thanks for the suggestion though :)
Re: SLF4J - Pulling Hair Out Trying To Configure
Nov 2, 2009 3:01 PM
(reply 4
of 38) (In reply to
#3 )
Yes, it returns the correct path (I've manually checked it in Windows Explorer and I have a .exists() check within my code to make sure the file is there). There's no such thing as a silly question! :)
amp88 wrote:
I would get a no class definition exception and the application would fall over into a heap rather than running but giving a warning about the configuration.
Not true. Logging facades such as SLF4J and commons-logging are quite happy to start up without having anything to delegate logging to. The fact that you didn't just say "yes it's there" makes me suspect it isn't. Check that.
Re: SLF4J - Pulling Hair Out Trying To Configure
Nov 3, 2009 9:39 AM
(reply 7
of 38) (In reply to
#6 )
If I just export the JAR normally in eclipse (i.e. without the libraries included) then attempt to run the application I do get a ClassNotFoundException for SLF4J:
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
I want to try to get this working by using a single JAR file as I'm setting it as my codebase path for my application (it's an RMI app).
amp88 wrote:
If I just export the JAR normally in eclipse (i.e. without the libraries included) then attempt to run the application I do get a ClassNotFoundException for SLF4J:
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
Well you will do. You still need to provide the jars to the classpath, one way or another.
I want to try to get this working by using a single JAR file as I'm setting it as my codebase path for my application (it's an RMI app).
There's nothing you've said so far that means you have to deploy your code as a single jar. Most software beyond the most trivial, uses some sort of dynamic linking, be that dlls or jars or shared objects or whatever. It makes things easier.
The point I was trying to make about the NoClassDef was that the libraries were correctly set on the classpath in the Fat Jar (otherwise the app would fall over as I said before).
Re: SLF4J - Pulling Hair Out Trying To Configure
Nov 3, 2009 11:01 AM
(reply 10
of 38) (In reply to
#9 )
amp88 wrote:
The point I was trying to make about the NoClassDef was that the libraries were correctly set on the classpath in the Fat Jar (otherwise the app would fall over as I said before).
No. What you've proven is that your code, alone, in an ordinary jar, won't work as it has dependencies on, at least, the SLF4J API, and probably others. The point I was making was that your application will still run happily if SLF4J is missing the adapter for log4j.
edit: you are aware that there are three libraries involved in using SLF4J, right? The SLF4J API, the delegate framework, and the SLF4J adapter.
I've now exported a normal JAR with eclipse (i.e. without the libraries) and added those libraries to the manifest so the manifest now looks like this:
I've taken the Properties.setProperty() call out for the log4j properties and I've just put the "log4j.properties" (i.e. default name, should be picked up) file in the same dir as the new JAR file. What am I missing here?
I've now exported a normal JAR with eclipse (i.e. without the libraries) and added those libraries to the manifest so the manifest now looks like this:
I've taken the Properties.setProperty() call out for the log4j properties and I've just put the "log4j.properties" (i.e. default name, should be picked up) file in the same dir as the new JAR file. What am I missing here?
How are you starting the jar? Where are those dependencies? Personally, I don't rely on specifying the classpath in the manifest, I prefer explicitly setting the classpath as arguments to the JRE. That eliminates a lot of classpath issues.