简体   繁体   English

在 jar 文件上通过命令行运行时出现 SLF4J.NoClassDefFoundError

[英]SLF4J .NoClassDefFoundError when running by command line on jar file

I have an app that runs well if I start it using the Intellij button after a mvn clean install (all using the GUI).如果我在 mvn clean install(全部使用 GUI)之后使用 Intellij 按钮启动它,我有一个运行良好的应用程序。

I try to run the generated jar file using the command line:我尝试使用命令行运行生成的 jar 文件:

java -jar myJar.jar

But I can't stop running into these errors:但我不能停止遇到这些错误:

java -jar .\myJar-SNAPSHOT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
    at data.SecretProvider.<clinit>(SecretProvider.java:14)

Again, if I run the app using Intellij, no errors and the logs work.同样,如果我使用 Intellij 运行应用程序,则不会出现错误并且日志可以正常工作。

I think it's the way I package my jar in the pom.xml, but I have very to no knowledge about it.我认为这是我在 pom.xml 中的 package 我的 jar 的方式,但我对此一无所知。 Here's the pom.xml这是 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">
<modelVersion>4.0.0</modelVersion>

<groupId>org.bouji</groupId>
<artifactId>myjar</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
</properties>

<repositories>
    <repository>
        <id>dv8tion</id>
        <name>m2-dv8tion</name>
        <url>https://m2.dv8tion.net/releases</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>net.dv8tion</groupId>
        <artifactId>JDA</artifactId>
        <version>5.0.0-alpha.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.7</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.7</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>2.7</version>
    </dependency>
    <dependency>
        <groupId>com.sedmelluq</groupId>
        <artifactId>lavaplayer</artifactId>
        <version>1.3.77</version>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk</artifactId>
        <version>1.11.163</version>
    </dependency>
    <dependency>
        <groupId>org.jooq</groupId>
        <artifactId>jool</artifactId>
        <version>0.9.14</version>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>2.21.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-java-sdk</artifactId>
        <version>1.11.960</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>CatBot</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

Tried reading similar issues on here, but it seems to always be specific to the error/ide.尝试在这里阅读类似的问题,但它似乎总是特定于错误/ide。

In jar's MANIFEST file there must be dependencies that needed for the application to run.在 jar 的 MANIFEST 文件中,必须有应用程序运行所需的依赖项。 Ensure that you have them.确保您拥有它们。

Also -jar doesn't include classpath other than file itself.此外-jar不包括文件本身以外的类路径。 You can try to run with:您可以尝试运行:

java -cp <path>myJar-SNAPSHOT.jar;<path to missing> <your main class>

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

相关问题 在jar文件中配置SLF4J - Config SLF4J in jar file 在IntelliJ中运行测试时播放Framework java.lang.NoClassDefFoundError:org / slf4j / LoggerFactory - Play Framework java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory when running tests in IntelliJ 如何在运行java jar应用程序时修复“java.lang.NoClassDefFoundError:org / slf4j / LoggerFactory”错误 - how to fix “java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory” error when run java jar application 将slf4j jar文件放在Glassfish 4中的哪里? - Where to put slf4j jar file in Glassfish 4? NoClassDefFoundError: org/slf4j/LoggerFactory with logback - NoClassDefFoundError: org/slf4j/LoggerFactory with logback NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder - NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder NoClassDefFoundError:org / slf4j / Logger - NoClassDefFoundError: org/slf4j/Logger NoClassDefFoundError: org/slf4j/LoggerFactory 在使用 gradle 创建可运行的 *.jar 时 - NoClassDefFoundError: org/slf4j/LoggerFactory while creating a runnable *.jar with gradle 文件未创建slf4j - File is not creating slf4j SLF4J:无法加载类“org.slf4j.impl.StaticLoggerBinder”。 在运行JUnit测试时 - SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”. when running JUnit test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM