0

We have a java app which is loading a logback.xml file from the classpath. How do we tell the exact location of the jar/directory from where that file is loaded?

I tried this code snippet in main():

 URL[] urLs = ((URLClassLoader) (Thread.currentThread().getContextClassLoader())).getURLs();
 for (URL url: urLs) {
    System.out.println("classloader url="+url);
 }

It prints all the jars loaded by the classloader:

classloader url=jar:file:/my-service.jar!/BOOT-INF/classes!/
classloader url=jar:file:/my-service.jar!/BOOT-INF/lib/spring-boot-2.5.2.jar!/
classloader url=jar:file:/my-service.jar!/BOOT-INF/lib/spring-boot-actuator-2.5.2.jar!/
classloader url=jar:file:/my-service.jar!/BOOT-INF/lib/spring-cloud-starter-bootstrap-3.0.3.jar!/
classloader url=jar:file:/my-service.jar!/BOOT-INF/lib/spring-cloud-starter-3.0.3.jar!/
classloader url=jar:file:/my-service.jar!/BOOT-INF/lib/spring-cloud-context-3.0.3.jar!/

Thank you.

user674669
  • 10,681
  • 15
  • 72
  • 105

1 Answers1

0

How do we tell the exact location of the jar/directory from where that file is loaded?

It is in the URLs.

jar:file:/my-service.jar!/BOOT-INF/classes!/

means the directory "/" in the directory "/BOOT-INF/classes" in the JAR file given by the URL "file:/my-service.jar"

jar:file:/my-service.jar!/BOOT-INF/lib/spring-boot-2.5.2.jar!/

means the directory "/" in the JAR file "/BOOT-INF/lib/spring-boot-2.5.2.jar" in the JAR file given by the URL "file:/my-service.jar".

And so on.

What is actually going on here is that the Spring Boot classloader understands how to find things in JARs that are nested within other JARs.

If you want to find where a specific resource is going to be loaded from, you can call classLoader.getResource(resourcePath) to get the URL, and then interpret the URL as above.

If I am reading https://logback.qos.ch/manual/configuration.html correctly, the default resource path will be "/logback-test.xml" followed by "/logback.xml". But this can be overridden in various ways.

You should also be able to track down where Logback is getting its configs from by setting a system property; see How can I determine what log configuration source Logback actually used?

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216