Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to fix "java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory" error when run java jar application

we are using eclipse+spring tool suite to build java application, we can start the application in eclipse IDE, but if we export as jar with all dependency jars, and run it on linux machine. we always gets error as following:

Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
    at com.ctrip.framework.apollo.demo.api.SimpleApolloConfigDemo.<clinit>(SimpleApolloConfigDemo.java:22)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

here is our POM.xml below:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>apollo</artifactId>
        <groupId>com.ctrip.framework.apollo</groupId>
        <version>1.3.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>apollo-demo</artifactId>
    <name>Apollo Demo</name>
    <packaging>jar</packaging>
    <properties>
        <java.version>1.7</java.version>
        <github.path>${project.artifactId}</github.path>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.ctrip.framework.apollo</groupId>
            <artifactId>apollo-client</artifactId>
            <version>${project.version}</version>
        </dependency>
        <!-- for spring demo -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <!-- for spring boot demo -->
        <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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
    <!-- for refresh scope demo -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
        </dependency>
        <!-- take over jcl -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-api</artifactId>
             <version>1.7.25</version>
        </dependency>        
    </dependencies>
</project>

2.1 we already included slf4j-log4j12 and slf4j-api as depdency.

2.2 the application can run normally in eclipse IDE window.

2.3 if we export executable jar including all dependencies, and ship jar on linux machine, run the jar file will get the above error.

2.4 we also checked generated jar package on windows with winRAR, the slf4j and log4j* jar package were there, see picture below.

enter image description here

we have struggled this for half day, but did not get any progress. Hope each expert can share with us some light or any suggestions. really appreciated!!!

like image 335
arden Avatar asked Oct 22 '25 17:10

arden


1 Answers

You need to tell maven to compile the dependencys like this, the jars are compiled but you need the src in youre jar and not the jars in there:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

and dependencys normaly have a <scope>compile</scope> tag, to tell the compile that they are needed at runtime

like image 176
Starmixcraft Avatar answered Oct 24 '25 07:10

Starmixcraft



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!