简体   繁体   English

Java:线程“ main”中的异常java.lang.NoClassDefFoundError:org / slf4j / LoggerFactory

[英]Java: Exception in thread “main” java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory

I have a project structured as 我有一个结构如下的项目

myproject/
         moduleA/
         moduleB/
         moduleC/

myproject has pom.xml as myproject具有pom.xml作为

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.4</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.0.3</version>
            <scope>provided</scope>
        </dependency>

Now moduleC needs moduleB code so it references the dependency as 现在moduleC需要moduleB代码,因此它引用依赖项为

       <dependency>
            <groupId>com.org.myproject</groupId>
            <artifactId>moduleB</artifactId>
            <version>${project.version}</version>
        </dependency>

But when I execute the class in moduleC , it complains 但是当我在moduleC执行该类时,它抱怨

Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
......
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory

This happens when my moduleC class executes moduleB code. 当我的moduleC类执行moduleB代码时,会发生这种情况。

What is that I am doing wrong? 我做错了什么? how can I fix this? 我怎样才能解决这个问题?

You are setting your dependencies to 'provided' which means that they won't be included in your runtime classpath. 您将依赖项设置为“提供”,这意味着它们将不会包含在您的运行时类路径中。 You are basically telling Maven that you will provide these files at runtime, so they are there for compilation, but unless you put them in your classpath manually they won't be there when you run. 您基本上是在告诉Maven,您将在运行时提供这些文件,因此它们可以在此处进行编译,但是除非手动将它们放在类路径中,否则它们将在运行时不存在。

Have a look here: http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope for more information on each of the scope levels. 在此处查看: http : //maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope ,以获取有关每个范围级别的更多信息。

If you specify nothing then the scope will be compile: 如果您不指定任何内容,则范围将被编译:

Compile is the default scope, used if none is specified. 编译是默认作用域,如果未指定,则使用它。 Compile dependencies are available in all classpaths of a project. 编译依赖项在项目的所有类路径中均可用。 Furthermore, those dependencies are propagated to dependent projects. 此外,这些依赖项会传播到相关项目。

So you can omit your scope tag or explicitly add it as compile and when you run your app the dependencies will be included on the runtime classpath. 因此,您可以省略范围标记或将其明确添加为编译标记,并且在您运行应用程序时,依赖项将包含在运行时类路径中。

as per comment by @Dave, I did added the following to my pom.xml and things started to work as usual 根据@Dave的评论,我确实在pom.xml添加了以下内容,一切开始照常工作

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.6</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.0.6</version>
        </dependency>

暂无
暂无

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

相关问题 RabbitMQ - 线程“main”中的异常java.lang.NoClassDefFoundError:org / slf4j / LoggerFactory - RabbitMQ - 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 Gradle-线程“ main”中的异常java.lang.NoClassDefFoundError:org / slf4j / LoggerFactory - Gradle - Exception in thread “main” java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory XUGGLE ERROR:线程“main”中的异常java.lang.NoClassDefFoundError:org / slf4j / LoggerFactory - XUGGLE ERROR: Exception in thread “main” java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory 线程“ main”中的Gradle异常java.lang.NoClassDefFoundError:org / slf4j / LoggerFactory - Gradle Exception in thread “main” 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.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 java.lang.NoClassDefFoundError: OSGi 中的 org/slf4j/LoggerFactory - java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory in OSGi JasperReports:出现“ java.lang.NoClassDefFoundError:org / slf4j / LoggerFactory”异常 - JasperReports: getting “java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory” Exception
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM