1

I have packaged my Spring-boot application with the spring-boot-maven-plugin. I use one library, which searches for some files on the classpath. Therefore I need to add additional resources directories on the classpath. I am using following command to run the application:

java -cp "C:\temp\some-dir;C:\temp\another-dir;my-application-1.0.jar" org.springframework.boot.loader.JarLauncher

The application starts and runs successfully, however the log4j2 configuration is not used. When I check the archive, the log4j2.xml is copied into the BOOT-INF/classes, which is the last entry in the classloader. I guess, that is why the log4j2 is not configured correctly.

But when I run the application through java -jar, omitting the additional classpath, the log4j2 configuration is successfully set up.

My only workaround so far is to specify the logging configuration externally as -Dlogging.config=classpath:log4j2.xml, but I would like to have everything encapsulated.

Any ideas how to make this work?

Filip Majernik
  • 7,700
  • 13
  • 45
  • 53
  • Why library searches files on classpath? To me it appears as misusing of classpath. Probably it is better to introduce parameter for searchpath so it does not clash with classpath. Nevertheless, it is still interesting why log4j is broken. – Alexander Pavlov Nov 26 '19 at 14:31
  • 1
    Add to application.properties (or yaml) in resources logging.config=classpath:log4j2.xml (https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html) – simar Nov 26 '19 at 14:42
  • @Alexander: you're right. But the library was meant for unit testing, but contains a functionality that I want to reuse (after all that is what programming is about) – Filip Majernik Nov 26 '19 at 15:08
  • @Simar: Yes, this is exactly what I have written in the description. – Filip Majernik Nov 26 '19 at 15:08
  • Can be occasionally some of classpath directories contains logging configuration, like logback.xml – simar Nov 26 '19 at 15:27
  • JarLauncher as per documentation able to use only jar archives as source of resources. – simar Nov 26 '19 at 15:28
  • Try to use loader.path for JarLauncher https://stackoverflow.com/questions/40499548/how-to-configure-additional-classpath-in-springboot – simar Nov 26 '19 at 15:28
  • @Filip Majernik, Simar's idea allows to embed property into resources without using `-D`. Did you try that? – Alexander Pavlov Nov 26 '19 at 15:43
  • @simar: I've checked the loader.path, but unfortunately that is only for the libraries, not all resources. It does not work with the log4j2 configuration file. – Filip Majernik Nov 27 '19 at 12:24
  • @AlexanderPavlov: aaa, this could work. You mean by putting the variable into application.properties? But isn't that processed by the Spring only? I think the log4j gets initialized before Spring. – Filip Majernik Nov 27 '19 at 12:26
  • it does but Spring initializes it again, if I remember correctly. – Alexander Pavlov Nov 27 '19 at 13:27

0 Answers0