3

运行 Jar(Maven 项目)后,我得到以下信息:

java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory

该错误是在变量的声明中引起的:

private static final Logger logger = LoggerFactory.getLogger("name.log");

这个错误是因为我需要在项目classPath中添加一个slf4j实现(这是我经过几个小时的搜索才理解的)

我在线阅读的解决方案建议向 Pom 添加一个依赖项,它应该解决它。好吧,由于某种原因,它对我不起作用。

因为它是一个 Maven 项目,所以我的 pom.xml 看起来像这样:

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.13</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.13</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.13</version>
    </dependency>

    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.1.3</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>1.1.3</version>
    </dependency>

可能相关:当我在 Netbeans (IDE) 中运行项目时,它会完美记录;但是一旦我构建并尝试运行 jar,它就会抛出上面提到的异常。

我很感激帮助。

4

1 回答 1

-2

它与这个问题无关,但只保留一个 slf4 实现,例如 slf4j-simple。

现在关于丢失的 LoggerFactory。在应用程序服务器外部运行时,必须通过显式类路径或通过在生成的 jar(fatjar)中嵌入所需的 jar 档案来提供库。

对于前一个选项,可以在命令行上或使用 jar 清单来完成。

下面是一个 pom,显示了 manifest 的使用和 fatjar 的生成:

  • 无需打包,使用 Maven 类路径:

mvn clean compile antrun:run -Pclass-antrun

  • 在清单中使用类路径打包:

mvn clean package antrun:run -Pjar-antrun

  • 包装一个fatjar:

mvn clean package antrun:run -Pfatjar

<?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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>dummy-slf4j</groupId>
    <artifactId>dummy-slf4j</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <main.class>dummy.App</main.class>

        <jdk.version>1.6</jdk.version>
        <project.encoding>UTF-8</project.encoding>

        <project.build.sourceEncoding>${project.encoding}</project.build.sourceEncoding>
        <project.reporting.outputEncoding>${project.encoding}</project.reporting.outputEncoding>

        <maven.compiler.source>${jdk.version}</maven.compiler.source>
        <maven.compiler.target>${jdk.version}</maven.compiler.target>
        <maven.compiler.compilerVersion>${jdk.version}</maven.compiler.compilerVersion>
        <maven.compiler.fork>true</maven.compiler.fork>
        <maven.compiler.verbose>true</maven.compiler.verbose>
        <maven.compiler.optimize>true</maven.compiler.optimize>
        <maven.compiler.debug>true</maven.compiler.debug>

        <maven.jar.plugin.version>2.6</maven.jar.plugin.version>
        <maven.assembly.plugin.version>2.6</maven.assembly.plugin.version>
        <maven.dependency.plugin.version>2.10</maven.dependency.plugin.version>
        <maven.antrun.plugin.version>1.8</maven.antrun.plugin.version>

    </properties>

    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.13</version>
        </dependency>
    </dependencies>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>${maven.jar.plugin.version}</version>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainClass>${main.class}</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>${maven.antrun.plugin.version}</version>
                </plugin>
                <plugin>
                    <artifactId>maven-assembly-plugin</artifactId>
                    <version>${maven.assembly.plugin.version}</version>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                        <archive>
                            <manifest>
                                <mainClass>${main.class}</mainClass>
                            </manifest>
                        </archive>
                    </configuration>
                    <executions>
                        <execution>
                            <id>make-assembly</id>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
                <plugin>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <version>${maven.dependency.plugin.version}</version>
                    <executions>
                        <execution>
                            <id>copy-dependencies</id>
                            <phase>package</phase>
                            <goals>
                                <goal>copy-dependencies</goal>
                            </goals>
                            <configuration>
                                <includeScope>runtime</includeScope>
                                <outputDirectory>${project.build.directory}/lib/</outputDirectory>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>

    </build>

    <profiles>
        <profile>
            <id>jar-antrun</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-dependency-plugin</artifactId>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-jar-plugin</artifactId>
                        <configuration>
                            <archive>
                                <manifest>
                                    <addClasspath>true</addClasspath>
                                    <classpathPrefix>lib/</classpathPrefix>
                                </manifest>
                            </archive>
                        </configuration>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <configuration>
                            <target>
                                <java fork="true"
                                    jar="${project.build.directory}/${project.build.finalName}.jar" />
                            </target>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>class-antrun</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <configuration>
                            <target>
                                <java fork="true" classname="${main.class}">
                                    <classpath refid="maven.compile.classpath" />
                                </java>
                            </target>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
        <profile>
            <id>fatjar</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-assembly-plugin</artifactId>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <configuration>
                            <target>
                                <java fork="true"
                                jar="${project.build.directory}/${project.build.finalName}-jar-with-dependencies.jar" />
                            </target>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

使用主应用程序:

package dummy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class App {
    private static final Logger logger = LoggerFactory.getLogger(App.class);
    public static void main(String[] args) {
        logger.info("toto");
    }
}

更新:为 fatjar 添加配置文件

于 2016-01-15T04:44:36.783 回答