I am new to Java logging API and need some help with this problem:
While creating the application, my config file was stored in the project root folder, so I used -Djava.util.logging.config.file=logging.properties
switch to run the program.
But then I exported the executable JAR.
How to configure the logging now? It doesn't work, when I specify the path to config file with the -D switch.
jar doesn't use any logging framework. In that case, it is impossible to get logs from it. However, what you can do is use a debugger. All IDEs have debuggers.
To enable logging perform the following actions: Open Java Control Panel. Click Advanced tab. Select Enable Logging under the Debugging option.
Normally, we put the logging. properties at the src/main/resources , and project compile or build will copy it to the root of the classpath. And we can use LogManager or System. setProperty to load the logging.
I know it's little late to answer this question but I ran into this issue few days back with the runnable jar and I solved it like this:
java -Djava.util.logging.config.file=logging.properties -cp test.jar com.sample.test.Main
where test.jar is the name of your jar and com.sample.test.Main is the fully qualified name of your main class.
The javadoc says:
In addition, the LogManager uses two optional system properties that allow more control over reading the initial configuration:
"java.util.logging.config.class" "java.util.logging.config.file"
These two properties may be set via the Preferences API, or as command line property definitions to the "java" command, or as system property definitions passed to JNI_CreateJavaVM.
If the "java.util.logging.config.class" property is set, then the property value is treated as a class name. The given class will be loaded, an object will be instantiated, and that object's constructor is responsible for reading in the initial configuration. (That object may use other system properties to control its configuration.) The alternate configuration class can use readConfiguration(InputStream) to define properties in the LogManager.
So, either use the java.util.logging.config.file
system property, and store the config file out of the jar file (which is probably a good idea if you want to be able to customize the logging properties in order to debug or analyze some weird behavior), or store the config file wherever you want (in the jar file, for example), and use the java.util.logging.config.class
system property to load and instantiate a class that will read the file in the jar file (Using Class.getResourceAsStream()
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With