How to read log4j configuration file in Java project summarized in jar file Memo

Overview

When I put together a program that outputs logs using log4j into a jar file, log4j.xml, which is the log4j configuration file, did not load properly.

The structure of the project looks like this.

├ src
  └ foo 
     └ foo.java <=Main program
└ resources
  └ log4j.xml <=log4j config file

Failure example

In the constructor of the main program, I read the configuration file as follows.

foo.java


import org.apache.log4j.xml.DOMConfigurator;

public class foo {
    public foo () {
        DOMConfigurator.configure("log4j.xml");
    }
}

By specifying the file name log4j.xml, when I test it on Eclipse, it loads well, but when I run this project as a jar file, it fails with an error that the file cannot be found.

Cause of failure

When I made the project into a jar file and called it from another program, it looked for the file log4j.xml in the startup directory of that program, so it seems that it could not be found successfully.

Therefore, instead of specifying only the file name, specify the absolute path to the file.

solution

By rewriting the constructor as follows, you can specify the absolute path, and even if you put the project together in a jar file, the configuration file will be read well.

foo.java


	public foo () {
		ClassLoader loader = Thread.currentThread().getContextClassLoader();
		DOMConfigurator.configure(loader.getResource("log4j.xml"));
	}

Recommended Posts

How to read log4j configuration file in Java project summarized in jar file Memo
How to read your own YAML file (*****. Yml) in Java
How to add jar file in ScalaIDE
How to create a new Gradle + Java + Jar project in Intellij 2016.03
How to convert a file to a byte array in Java
How to debug the generated jar file in Eclipse
Memo: [Java] How to check groupId etc. described in pom.xml
Read Java properties file in C #
How to learn JAVA in 7 days
Log output to file in Java
How to use classes in Java?
How to name variables in Java
How to concatenate strings in java
How to make a jar file with no dependencies in Maven
Sample to read and write LibreOffice Calc fods file in Java 2021
How to get the length of an audio file in java
How to implement date calculation in Java
How to implement Kalman filter in Java
Multilingual Locale in Java How to use Locale
Try to solve Project Euler in Java
[Java] How to use the File class
How to do base conversion in Java
How to implement coding conventions in Java
How to embed Janus Graph in Java
Read xlsx file in Java with Selenium
How to get the date in java
Sample to unzip gz file in Java
(Memo) How to solve dummy output in Ubuntu 20.04
Read a string in a PDF file with Java
How to display a web page in Java
How to hide null fields in response in Java
How to ZIP a JAVA CSV file and manage it in a Byte array
[Java] Read the file in src / main / resources
How to bind to property file in Spring Boot
[Java] How to substitute Model Mapper in Jackson
How to solve an Expression Problem in Java
[Java] Memo on how to write the source
How to build an executable jar in Maven
How to write Java String # getBytes in Kotlin?
How to save a file with the specified extension under the directory specified in Java to the list
[Java] [Maven] Template of pom.xml to register 3rd party jar file in local repository
How to call functions in bulk with Java reflection
How to create a Java environment in just 3 seconds
[Java] How to omit the private constructor in Lombok
How to jump from Eclipse Java to a SQL file
How to input / output IBM mainframe files in Java?
How to create a Spring Boot project in IntelliJ
Java memory management and how to read GC Viewer
How to create a data URI (base64) in Java
Add an external jar file in your IntelliJ project.
[Java] Get the file in the jar regardless of the environment
How to test file upload screen in Spring + Selenium
How to convert A to a and a to A using AND and OR in Java
How to Git manage Java EE projects in Eclipse
Summary of how to implement default arguments in Java
How to put old Java (8 series) in macOS 10.15 Catalina
Notes on how to use regular expressions in Java
Read JSON in Java
Read Java Property file
How to read a library from a JAR file with VS Code << How to not use Maven / Gradle >>
How to get the class name / method name running in Java