Issue
I am working on deploying a spring boot executable jar for my application. Im using SLF4J logging, and when I build and run inside of IntelliJ I have no issues.
However, when I try to run the .jar, from the command line I get a LoggerFactory is not a Logback LoggerContext but Logback is on the classpath exception.
It complains about the slf4j-log4j12-1.7.12.jar in two places /opt/mapr/lib/
and /opt/mapr/hadoop/hadoop-2.7.0/share/hadoop/common/lib/
.
If i remove the jar from both places and run my app:
java -cp $(mapr classpath):MapRProducerApp-0.0.1-SNAPSHOT.jar org.springframework.boot.loader.PropertiesLauncher
it will then fail startup due to SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
I dont understand why it would fail saying faild to load the slf4j class when my apps .jar is built with that dependency through
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
In IntelliJ, I am bringing in all external dependencies that exist inside the /opt/mapr/lib
folder, which includes the slf4j-log4j12-1.7.10.jar, but IntelliJ does not give the Logback LoggerContext error.
To recap here:
- Building and running the jar on its own works, but will fail because it needs dependencies that exist inside the
mapr classpath
. - Running the jar with the
mapr classpath
fails because of the slf4j jar inside /opt/mapr/lib. - Removing that jar leads to another failure about a slf4j jar in /opt/mapr/hadoop/hadoop-2.7.0/share/hadoop/common/lib/.
- Removing that jar then fails the app because it now cannot find any slf4j binding.
Is there something im missing? Do I need to package my app a certain way so it does not include the SLF4J dependency?
Solution
I'm not clear about your <dependencies>
hierarchy, but by adding <exclusion>
you can simply resolve the conflict.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
Answered By - Denuwan.hh