运行JAR时如何配置日志记录?

运行JAR时如何配置日志记录?

问题描述:

我是Java日志记录API的新手,需要一些有关此问题的帮助: 在创建应用程序时,我的配置文件存储在项目的根文件夹中,因此我使用-Djava.util.logging.config.file=logging.properties开关运行该程序. 但是后来我导出了可执行文件JAR. 现在如何配置日志记录?当我使用-D开关指定配置文件的路径时,它不起作用.

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.

您无法在MANIFEST.MF 文件中指定JVM参数,因此您必须在命令行中指定日志记录属性或快捷方式:

You can't specify JVM arguments into the MANIFEST.MF file so you have to specify the logging properties at the command line or with a shortcut :

java -Djava.util.logging.config.file=logging.properties -jar yourjar.jar

否则,您可以在JAR中打包属性文件(在您的情况下为logging.properties),请在以下位置阅读 启动,并将这些设置放入系统属性.

Otherwise you could package a properties file(logging.properties in your case) in the JAR, read that at startup and put those settings into the system properties.