简体   繁体   English

从命令行-java.lang.ClassNotFoundException:org.slf4j.LoggerFactory

[英]From command line - java.lang.ClassNotFoundException: org.slf4j.LoggerFactory

OK. 好。 I know there are other questions like this one out there and this is not the first time that slf4j has kicked my butt. 我知道还有其他这样的问题,这不是slf4j第一次踢我的屁股。 However, I have looked at my PATH in Environment Variables and below are the two slf4j jar files included in my PATH as well as my project dependencies. 但是,我在“环境变量”中查看了我的PATH,以下是我的PATH中包含的两个slf4j jar文件以及项目依赖项。

C:\Users\pdl\.m2\repository\org\slf4j\slf4j-api\1.7.13\slf4j-api-1.7.13.jar
C:\Users\pdl\.m2\repository\org\slf4j\slf4j-simple\1.7.13\slf4j-simple-1.7.13.jar

This is what is in my pom file: 这是我的pom文件中的内容:

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

Other applications we are running contain slf4j jar dependencies in the IDE but they are not listed in the pom file. 我们正在运行的其他应用程序在IDE中包含slf4j jar依赖项,但未在pom文件中列出。 I am so confused about where to put what that I can't see straight. 我对在哪里放置我看不见的东西感到困惑。

This is what the application dependencies look like: 这是应用程序依赖项的样子:

依赖项屏幕截图

I can run the application from the IDE (Netbeans) but I get the following error when I try to run from command prompt. 我可以从IDE(Netbeans)运行该应用程序,但是当我尝试从命令提示符运行时出现以下错误。

C:\Users\pdl\Projects\WeatherTestDrive>java -cp WeatherApp.jar;WeatherOpenWeatherMap.jar;WeatherClient.jar com.a2i.weatherclient.Client
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
        at com.a2i.weatherclient.Client.<clinit>(Client.java:22)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 1 more

Adding slf4j in my VM classpath and I still get the error. 在我的VM类路径中添加slf4j,仍然出现错误。

C:\Users\pdl\Projects\WeatherTestDrive>java -cp WeatherApp.jar;WeatherOpenWeatherMap.jar;WeatherClient.jar;C:\Users\pdl\.m2\repository\org\slf4j\slf4j-api\1.7.13\slf4j-api-1.7.13.jar;C:\Users\pdl\.m2\repository\org\slf4j\slf4j-simple\1.7.13\slf4j-simple-1.7.13.jar com.a2i.weatherclient.Client
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
        at com.a2i.weatherclient.Client.<clinit>(Client.java:22)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 1 more

Can somebody please help me figure out what I am doing wrong? 有人可以帮我弄清楚我在做什么错吗?

  1. Should I be adding slf4j-api to my dependencies instead of slf4j-exe? 我应该将slf4j-api而不是slf4j-exe添加到我的依赖项中吗? Or even something else? 甚至还有其他东西?
  2. I guess whichever one I use, I should add it to my VM classpath. 我想无论我使用哪一个,都应该将其添加到我的VM类路径中。
  3. Does it even need to be in my pom file? 它甚至需要放在我的pom文件中吗?

------------------------------ EDIT ---------------------------------- ------------------------------编辑------------------- ---------------

I created a simple HelloWorld app to log my name. 我创建了一个简单的HelloWorld应用来记录我的名字。 As soon as I added the Logger to my Hello class it was highlighted in red, so I added the slf4j-simple to my dependencies and slf4j-api came with it. 将Logger添加到Hello类后,它立即以红色突出显示,因此我将slf4j-simple添加到了我的依赖项中,并且slf4j-api也随之提供。 But when I opened the pom file only the slf4j-simple was added: 但是,当我打开pom文件时,仅添加了slf4j-simple:

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

When I run from the IDE, everything works well. 从IDE运行时,一切正常。 But when I run from command line I still get the error: 但是当我从命令行运行时,仍然出现错误:

C:\Users\pdl\Projects\HelloWorld\target>java -cp HelloWorld-1.0-SNAPSHOT.jar;C:\Users\pdl\.m2\repository\org\slf4j\slf4j-simple\1.7.13\slf4j-simple-1.7.13.jar com.a2i.helloworld.Hello
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
        at com.a2i.helloworld.Hello.<clinit>(Hello.java:17)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 1 more

Where's slf4j in your VM classpath? VM类路径中的slf4j在哪里? Try running as follows: 尝试按以下方式运行:

java -cp WeatherApp.jar;WeatherOpenWeatherMap.jar;WeatherClient.jar;C:\Users\pdl\.m2\repository\org\slf4j\slf4j-api\1.7.13\slf4j-api-1.7.13.jar;C:\Users\pdl\.m2\repository\org\slf4j\slf4j-simple\1.7.13\slf4j-simple-1.7.13.jar com.a2i.weatherclient.Client

You're missing slf4j from your runtime. 您在运行时中缺少slf4j。 Hope that helps. 希望能有所帮助。

Maven, and thus the pom-file, are intended to build the classpath for you. Maven以及pom文件旨在为您构建类路径。 So yes, all depedencies you like to use should be in your pom file. 因此,是的,您要使用的所有功能都应在pom文件中。

Regarding slf4j: slf4j-api is a dependency that only defines an api (or interface). 关于slf4j:slf4j-api是仅定义api(或接口)的依赖项。 To make it work you also have to add an implementation. 要使其工作,您还必须添加一个实现。 See here for explanation. 请参阅此处以获取解释。 So you have to add at least one more dependency. 因此,您必须再添加至少一个依赖项。 For example: 例如:

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

Regarding slf4j-ext, i am quite sure you dont need it to simply log. 关于slf4j-ext,我很确定您不需要它来简单地登录。 Maybe you have more elaborate use cases. 也许您有更详尽的用例。

To start your application from command line you have to run java with the -cp argument. 要从命令行启动应用程序,必须使用-cp参数运行java。 The classpath is a list all used classes or archives containing classes. 类路径是所有使用过的类或包含类的档案的列表。 For applications with a lot of dependencies it can become quiet cumbersome to build it manually. 对于具有大量依赖关系的应用程序,手动构建它会变得很麻烦。

Use mvn dependency:build-classpath to let maven build this big string of jar-paths for you. 使用mvn dependency:build-classpath可以让maven为您构建这个很大的jar路径字符串。

This concerns Java SE projects built in IntelliJ where slf4j - or any other library for that matter - is included (ie slf4j-api-1.7.26.jar => The API & slf4j-simple-1.7.26.jar => the API implementation)... 这涉及在IntelliJ中构建的Java SE项目,其中包含slf4j(或与此相关的任何其他库)(即slf4j-api-1.7.26.jar => API和slf4j-simple-1.7.26.jar => API实现)...

Step #1: 第1步:

create folder \\libs in project root and place the two JARs in there click to see project structure 在项目根目录中创建文件夹\\ libs并将两个JAR放在此处单击以查看项目结构

step #2: 第2步:

Build code and make sure that the Logger is working properly inside the IDE. 生成代码,并确保Logger在IDE中正常运行。

step #3: 步骤3:

Launch cmd and navigate using cd command inside the .\\out directory up until the packages of the main (check 1st line of Main.java). 启动cmd并使用。\\ out目录中的cd命令导航,直至找到main的软件包(检查Main.java的第一行)。 ie

cd C:\\Users.....\\out...\\testcp cd C:\\ Users ..... \\ out ... \\ testcp

step #4 第四步

Do right click on sjf4j-api jar and copy path (path#1) & 右键单击sjf4j-api jar并复制路径(path#1)&

Do right click on sjf4j-simple jar and copy path (path#2) & 右键单击sjf4j-simple jar并复制路径(path#2)&

Do right click on Main.class and copy path (path#3. SOS: EXCLUDE THE Main.class part). 右键单击Main.class并复制路径(路径#3。SOS:排除Main.class部分)。

Now, run java command with -cp flag as: 现在,使用-cp标志运行java命令,如下所示:

java -cp path#1;path#2;path#3; java -cp path#1; path#2; path#3; org.me.Main org.me.Main

That's it 而已

Note : Eclipse users do not run into this problem since this is taken care of by using the build path utility. 注意 :Eclipse用户不会遇到此问题,因为可以通过使用构建路径实用程序来解决此问题。

暂无
暂无

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

相关问题 引起:java.lang.ClassNotFoundException:org.slf4j.LoggerFactory - Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory DefaultHttpClient引发原因:java.lang.ClassNotFoundException:org.slf4j.LoggerFactory - DefaultHttpClient throws Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory ClassNotFoundException: org.slf4j.LoggerFactory - ClassNotFoundException: org.slf4j.LoggerFactory ClassNotFoundException:org.slf4j.LoggerFactory Maven - ClassNotFoundException: org.slf4j.LoggerFactory Maven java.lang.NoClassDefFoundError: org.slf4j.LoggerFactory - java.lang.NoClassDefFoundError: org.slf4j.LoggerFactory Android Studio Jbox2D java.lang.NoClassDefFoundError:org.slf4j.LoggerFactory - Android Studio Jbox2D java.lang.NoClassDefFoundError: org.slf4j.LoggerFactory java.lang.NoClassDefFoundError:无法初始化类org.slf4j.LoggerFactory - java.lang.NoClassDefFoundError: Could not initialize class org.slf4j.LoggerFactory java.lang.IllegalAccessError:试图从类 org.slf4j.LoggerFactory 访问字段 org.slf4j.impl.StaticLoggerBinder.SINGLETON - java.lang.IllegalAccessError: tried to access field org.slf4j.impl.StaticLoggerBinder.SINGLETON from class org.slf4j.LoggerFactory java.lang.IllegalAccessError: 试图从 SpringBoot 中的 org.slf4j.LoggerFactory 类访问字段 org.slf4j.impl.StaticLoggerBinder.SINGLETON - java.lang.IllegalAccessError: tried to access field org.slf4j.impl.StaticLoggerBinder.SINGLETON from class org.slf4j.LoggerFactory in SpringBoot java.lang.ClassNotFoundException:org.slf4j.Logger - java.lang.ClassNotFoundException: org.slf4j.Logger
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM