简体   繁体   English

如何在运行java jar应用程序时修复“java.lang.NoClassDefFoundError:org / slf4j / LoggerFactory”错误

[英]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. 我们使用eclipse + spring工具套件来构建java应用程序,我们可以在eclipse IDE中启动应用程序,但是如果我们将jar导出为所有依赖jar,并在linux机器上运行它。 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: 这是我们的POM.xml:

<?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.1我们已经将slf4j-log4j12和slf4j-api作为depdency包含在内。

2.2 the application can run normally in eclipse IDE window. 2.2应用程序可以在eclipse IDE窗口中正常运行。

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.3如果我们导出包含所有依赖项的可执行jar,并在linux机器上运送jar,运行jar文件将得到上述错误。

2.4 we also checked generated jar package on windows with winRAR, the slf4j and log4j* jar package were there, see picture below. 2.4我们还检查了winRAR窗口上生成的jar包,slf4j和log4j * jar包都在那里,见下图。

在此输入图像描述

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!!! 非常感谢!!!

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: 你需要告诉maven编译像这样的依赖项,jars是编译的,但是你需要你的jar中的src而不是那里的jar:

<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 和dependencys正常有一个<scope>compile</scope>标记,告诉编译它们在运行时需要它们

I've faced a similar problem few days ago, right I add the jar libraries but the core (scripts/classes) of the jar file are no included due some reason I was able to solve that problem by right click on the jar file go to properties and set the class path for the classes of that jar folder. 我几天前遇到过类似的问题,我添加了jar库但是没有包含jar文件的核心(脚本/类),因为某些原因我可以通过右键单击jar文件来解决这个问题go到属性并设置该jar文件夹的类的类路径。

if you wanna check that the jar file has no class Def inside it explore the jar file from the explorer and double click on any class you'll see a page showing up and says there's no script for this class and it asks you to set the class path. 如果您想检查jar文件中没有类Def,请从资源管理器中浏览jar文件,然后双击您将看到显示的页面的任何类,并说该类没有脚本,它会要求您设置阶级路径。

Another approach I tried which also worked for me is I've imported the same jar files on netbeans and they worked 100% 我尝试的另一种方法也适用于我,我在netbeans上导入了相同的jar文件,并且它们100%工作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Slf4j 和 Logback 错误:java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory - Slf4j and Logback error: java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory java.lang.NoClassDefFoundError: OSGi 中的 org/slf4j/LoggerFactory - java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory in OSGi 休眠-java.lang.NoClassDefFoundError:org / slf4j / LoggerFactory - Hibernate - java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory 问题 - java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory problem XUGGLE ERROR:线程“main”中的异常java.lang.NoClassDefFoundError:org / slf4j / LoggerFactory - XUGGLE ERROR: Exception in thread “main” java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory 在IntelliJ中运行测试时播放Framework java.lang.NoClassDefFoundError:org / slf4j / LoggerFactory - Play Framework java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory when running tests in IntelliJ 如何解决java.lang.NoClassDefFoundError:org / slf4j / LoggerFactory? - How can I resolve java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory? Java + Maven +在线程“main”中生成可执行jar +异常java.lang.NoClassDefFoundError:org / slf4j / LoggerFactory - Java + Maven + make executable jar + Exception in thread “main” java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory Java:线程“ main”中的异常java.lang.NoClassDefFoundError:org / slf4j / LoggerFactory - Java: Exception in thread “main” java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory Spring工具套件中的线程“ main”中的异常java.lang.NoClassDefFoundError:org / slf4j / LoggerFactory - Exception in thread “main” java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory in Spring tool suite
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM