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

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:
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?
Should I be adding slf4j-api to my dependencies instead of slf4j-exe? Or even something else?
I guess whichever one I use, I should add it to my VM classpath.
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

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.

Maven, and thus the pom-file, are intended to build the classpath for you.
So yes, all depedencies you like to use should be in your pom file.
Regarding slf4j:
slf4j-api is a dependency that only defines an api (or interface). 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. Maybe you have more elaborate use cases.
To start your application from command line you have to run java with the -cp argument. 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.

This concerns Java SE projects built in IntelliJ where slf4j - or any other library for that matter - is included (i.e. slf4j-api-1.7.26.jar => The API & slf4j-simple-1.7.26.jar => the API implementation)...
Step #1:
create folder \libs in project root and place the two JARs in there click to see project structure
step #2:
Build code and make sure that the Logger is working properly inside the IDE.
step #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). i.e.
cd C:\Users.....\out...\testcp
step #4
Do right click on sjf4j-api jar and copy path (path#1) &
Do right click on sjf4j-simple jar and copy path (path#2) &
Do right click on Main.class and copy path (path#3. SOS: EXCLUDE THE Main.class part).
Now, run java command with -cp flag as:
java -cp path#1;path#2;path#3; 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.

Related

Getting Exception in Data Driven Framework Scenario while using Apache POI

Getting Exception in Data Driven Framework Scenario while using Apache POI
I am getting the exception when I execute a scenario using datadriven framework with Apache POI. I am not sure if its a version issue, can anyone please take a look at the exception below to see what is causing this issue and any resolution. Let me know if need anymore details.
Versions:
Eclipse: Oxygen.3a Release (4.7.3a)
WebDriver: 3.4.0
POI: 3.17
As a work around I have added dom4j-1.6.jar and xmlbeans-xmlpublic-2.3.0.jar, which dint seem to solve the issue.
EXEPTION:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/xmlbeans/impl/common/SystemCache
at org.apache.poi.POIXMLDocument.init(POIXMLDocument.java:76)
at org.apache.poi.POIXMLDocument.<init>(POIXMLDocument.java:62)
at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:295)
at Carpet_Utility.ExcelUtils.setExcelfile(ExcelUtils.java:24)
at Carpet_Main.Main1.main(Main1.java:19)
Caused by: java.lang.ClassNotFoundException: org.apache.xmlbeans.impl.common.SystemCache
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)
... 5 more
Exception Screenshot
I think you have downloaded the wrong jar xmlbeans-xmlpublic-2.3.0.jar.
You have to download the xmlbeans-x.x.x.jar.
If you are using maven, then add this dependency in the pom.xml :
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>2.3.0</version>
</dependency>
or if you want to use jar, then download from here : mvnrepository
The screenshot of the org.apache.xmlbeans.impl.common.SystemCache class :
As i have already mentioned in a comments section that for apache poi functionality you have to add xmlbeans-2.3.0.jar dependency as well.
If you are using maven then you have to add this dependency in pom.xml along with dom4j-1.6.jar.
If you are using through jar dependency then you have to download this jar and then have to include in your project.

Maven Project can't import log4j2 classes

I created maven project, I addded log4j2 dependencies to POM, and my project obviously can't import LogManager class. Why? That's output from maven console.
c:\Users\Dawid\Desktop\Pracbaza\my-project>java -cp target\my-project-1.0-SNAPSHOT.jar com.spica.project.App
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/logging/log4j/LogManager
at com.spica.project.App.<clinit>(App.java:7)
Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.LogManager
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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
you've added those dependencies to the pom, so maven knows that your project depends on log4j, but you're not executing maven right now. you're using the java executable to run your compiled code. java has no idea what a pom.xml is (since maven is an external tool).
you have several options:
list all of your dependencies in the cp argument, not just your jar. so java -cp target\your.jar;path\to\log4j2.jar;anything;else
list your dependencies in your jar file's MANIFEST.MF file. you can either list absolute paths (BAD IDEA) or file names and place your libs alongside your jar
package your jar as either jar-with-dependencies or onejar. that way it'll pack all the libs alongside your own code and will be able to find them. there is a difference between these 2 options that you better understand before you pick one.
This is because your log4j jar is not there in the classpath while running your code. Try adding the log4j jar in your classpath (in -cp switch).

JUnit RemoteTestRunner NoClassDefFoundError Junit4 Eclipse 3.7.2

I am attemping to add JUnit tests to my existing Eclipse 3.7.2 project and it seems like JUnit is not properly added to the project. I have attempting adding the JUnit jar manually. I have tried several other run configuration modifications that all result in the same error. New projects that I create also exhibit this error when I add a JUnit test.
java.lang.NoClassDefFoundError: org/eclipse/jdt/internal/junit/runner/RemoteTestRunner
Caused by: java.lang.ClassNotFoundException: org.eclipse.jdt.internal.junit.runner.RemoteTestRunner
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
Exception in thread "main"
Are you installing Eclipse to a directory to which you can't write? Windows 7 UAC can prevent certain files from being written, especially in C:\Program Files.... It's better to install Eclipse in a directory that you can write.
This could be caused by a classpath issue. First ensure that you definitely have the junit.jar in your build path.
In Eclipse Indigo: Go to “Package Explorer” right-hand click > ‘Build Path’ > ‘Configure Build Path’ > in the Libraries tab. There you should the junit.jar.
Generally creating the test case within Eclipse adds this jar to the build path.
The same exception could also occur if you have a build path conflict. Going to Window > Show View > 'Markers' shows you the problems with the project. (In my case there was a circular reference)

Java: NoClassDefFoundError using Apache Commons CLI 1.2

I am using the Apache Commons CLI 1.2 to parse command line arguments in Java. I had run into the NoClassDefFoundError when trying to run my java class but solved it by fixing the class-paths.
Now I have the same exception but in regards to the actual commons-cli classes as is shown below:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/cl i/CommandLineParser Caused by:
java.lang.ClassNotFoundException: org.apache.commons.cli.CommandLineP arser
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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) Could not find the main class: com.amirsys.score.api.script.CMDContentPusher.
The only thing I could think of is to set the class path to the commons-cli jar but that did not work. I haven't been able to find a solution to fixing the NoClassDefFoundError for imported classes. I thought these were compiled into the .class files?
Commons CLI is not in the classpath at runtime. If you struggle to get the classpath right you can try to package your application as a single big jar file containing all its dependencies, including Commons CLI. There are many tools to achieve that (jarjar, onejar, Maven shade plugin...)

Java/eclipse/hibernate

I'm a .net dev, usually do this using nhibernate and can work with that fine. I'm doing a project which needs to be written in Java and have the following issue:
All I have so far is a blank java project setup in eclipse. I have C:\work\lib\java\cp\hibernate3.jar in the Referenced Libraries node in the package explorer.
in my code (main) i'm doing this
Configuration config = new Configuration().
setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect").
setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver").
setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:RefenceDb").
setProperty("hibernate.connection.username", "sa").
setProperty("hibernate.connection.password", "").
setProperty("hibernate.connection.pool_size", "1").
setProperty("hibernate.connection.autocommit", "true").
setProperty("hibernate.cache.provider_class", "org.hibernate.cache.HashtableCacheProvider").
setProperty("hibernate.hbm2ddl.auto", "create-drop").
setProperty("hibernate.show_sql", "true").
addClass(Boy.class);
and in the consol i have:
Exception in thread "main" java.lang.NoClassDefFoundError: org/dom4j/DocumentException
at ReferenceApplication.Main.main(Main.java:15)
Caused by: java.lang.ClassNotFoundException: org.dom4j.DocumentException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
... 1 more
any ideas?
the path C:\work\lib\java\cp\ is in the classpath
w://
While the other answers here referencing dom4j are correct, you will soon find there a dozen or so other dependencies.
Starting from total scratch is a noble pursuit, but I would actually recommend giving yourself a head start by installing Maven (for dependency management) and running mvn archetype:generate from the command line. If you're doing a web application (which you may not be based on the question text) selecting "maven-archetype-j2ee-simple" or "maven-archetype-webapp" would be a pretty good kickstart. Doing so will set up a project for you that can then be used in eclipse (you could even use m2eclipse plugin to do dependency management from within eclipse).
It will turn out that Maven has a learning curve of its own, but my opinion is that the benefits outweigh the costs.
Good luck!
Download dom4j and put it on your classpath (in the referenced libraries tab)
From the stack trace, it looks like you're missing dom4j.jar. Hibernate (for Java) has a ton of dependencies. Make sure that you have all of Hibernates required dependencies added to your Eclipse project.
You need to put the dom4j jar on your classpath. You should be able to get it here:
http://www.dom4j.org/download.html
I think that hibernate uses version 1.6.1
To make all this easier, I would quickly learn Maven and then use the m2eclipse plugin.
I know it's a learning curve, but it will make everything massively easier at deploy time.

Categories

Resources