Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

maven

slf4j

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. 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.

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:

    <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. I am so confused about where to put what that I can't see straight.

This is what the application dependencies look like:

Dependencies Screen Capture

I can run the application from the IDE (Netbeans) but I get the following error when I try to run from command prompt.

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.

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? Or even something else?
  2. I guess whichever one I use, I should add it to my VM classpath.
  3. Does it even need to be in my pom file?

------------------------------ EDIT ----------------------------------

I created a simple HelloWorld app to log my name. 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. But when I opened the pom file only the slf4j-simple was added:

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

When I run from the IDE, everything works well. 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
like image 996
Patricia Avatar asked May 29 '26 20:05

Patricia


1 Answers

Where's slf4j in your VM classpath? 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. Hope that helps.

like image 65
hd1 Avatar answered Jun 01 '26 08:06

hd1