arjun
arjun

Reputation: 51

Runnable JAR not detecting logback.xml configuration

I have a Java maven application in which I have created a swing GUI.

I am using logback-classic for logging purposes. logback.xml is present under the src/main/resources directory.

When I run the application using eclipse "Run As Java Application", it is working as intended and application is logging to the console and log file.

But after exporting the application as a runnable JAR file and double clicking to run the application, it is not logging to file.

When I opened command prompt and execute application using "java -jar myjarfile.jar", I am able to see the console logs, but it is using the default logback configuration.

I tried giving additional parameter, -Dlogback.configurationFile="path-to-file/logback.xml" and I am getting the below error,

Failed to instantiate [ch.qos.logback.classic.LoggerContext] Reported exception: java.lang.IllegalArgumentException: name at sun.misc.URLClassPath$Loader.findResource(Unknown Source) at sun.misc.URLClassPath$1.next(Unknown Source) at sun.misc.URLClassPath$1.hasMoreElements(Unknown Source) at java.net.URLClassLoader$3$1.run(Unknown Source) at java.net.URLClassLoader$3$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader$3.next(Unknown Source) at java.net.URLClassLoader$3.hasMoreElements(Unknown Source) at sun.misc.CompoundEnumeration.next(Unknown Source) at sun.misc.CompoundEnumeration.hasMoreElements(Unknown Source) at ch.qos.logback.core.util.Loader.getResources(Loader.java:73) at ch.qos.logback.classic.util.ContextInitializer.multiplicityWarning(ContextInitializer.java:183) at ch.qos.logback.classic.util.ContextInitializer.statusOnResourceSearch(ContextInitializer.java:175) at ch.qos.logback.classic.util.ContextInitializer.findConfigFileURLFromSystemProperties(ContextInitializer.java:111) at ch.qos.logback.classic.util.ContextInitializer.findURLOfDefaultConfigurationFile(ContextInitializer.java:120) at ch.qos.logback.classic.util.ContextInitializer.autoConfig(ContextInitializer.java:148) at org.slf4j.impl.StaticLoggerBinder.init(StaticLoggerBinder.java:84) at org.slf4j.impl.StaticLoggerBinder.(StaticLoggerBinder.java:55) at org.slf4j.LoggerFactory.bind(LoggerFactory.java:150) at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:124) at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:412) at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:357) at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:383) at com.esignature.tools.templatemigration.ConverterGUI.(ConverterGUI.java:35) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Unknown Source) at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:59)

I don't know what I am missing and I want to log from runnable JAR to the log file.

It is confusing because it is working from eclipse but not from the exported runnable JAR.

Below is the logback.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <property name="LOG_HOME" value="C:/template converter logs" />
    <appender name="STDOUT"
        class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level - %msg%n</pattern>
        </encoder>
    </appender>
    <appender name="rollingFile"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy
            class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/applog-%d{yyyy-MM-dd}.log</fileNamePattern>                        
        </rollingPolicy>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level - %msg%n</pattern>
        </encoder>
    </appender>     
    <root level="debug">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="rollingFile" />
    </root>
</configuration>

Upvotes: 5

Views: 2410

Answers (2)

arjun
arjun

Reputation: 51

Using maven shade plugin to generate a shaded JAR file that includes the dependencies and the custom logback.xml is one working solution that I utilized.

Now when I run this JAR file (either from command line or by double-clicking it), logs are generated in the specified location as expected.

Upvotes: 0

Kevin Valdez
Kevin Valdez

Reputation: 389

The error you are facing is because there is two logback.xml configuration, one is in your classpath and you are trying to override/add another one using -Dlogback.configurationFile

In my case when packaging the runnable JAR I changed the pom configuration to this.

<plugin>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-maven-plugin</artifactId>
   <configuration>
      <executable>true</executable>
   </configuration>
</plugin>

Note the executable set to true, this makes sure that a MANIFEST.MF file is added to the JAR package.

So in order to make this work with this config I added this property to my application.yml/application.properties

logging:
  config: file:D:\\LogConfig\\logback.xml

Then I proceed to generate the JAR file using clean, install mvn commands.

From there running the JAR file using java -jar myJar.jar works fine and there is no need to add arguments to search the application.yml or logback.xml. That is because the properties is being loaded from within the jar and the log config from the specified route on the application.yml .

Upvotes: 1

Related Questions