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).
Related
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.
I'm trying to use the apache http library in my project. I imported the libreries in my project http://imgur.com/WvwqcDS
When I run my program, I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/HttpEntity
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.getMainMethod(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.apache.http.HttpEntity
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)
... 6 more
The ClassLoader can't find org.apache.http.HttpEntity, which should be inside "httpcore-4.3.jar". Open the jar as an archive and verify that it contains org/apache/http/HttpEntity.class. If it does, the issue is in your launch configuration. In Eclipse, go to Run > Run Configurations... and find the Java Application profile for your main class (which should be the class containing the main method in "Launcher" project, based on the screenshot). Under the Classpath tab, you should see your library jars listed.
If not, go back to your "Launcher" project in the Navigation pane, right-click and select Properties, go to "Build Path", remove your jars, press OK, then go back in and add them again (to guarantee the eclipse meta-data is fresh). Also, under the "Order and Export" tab, it's a good idea to check off all jars so that if you include Launcher as a dependency for another project, the jars are transitively included.
your Eclipse is having trouble locating the external jars, try importing them into your workspace or referencing them outside by using "add external jar's"
In my case Maven was not updating dependencies properly. I use mvn clean and then re-updated dependencies then it got fixed.
I have 2 project in my eclipse work_space.
Project A:Source of Apache Tomcat by a little edit[and have some dependent jars].
Project B: A small project call one method[One method of project A's methods].
So when I used Project A in Project B(added to BuildPAth's projects) or test this method in project A that work. But When project B used exported project A's jar file DOESN'T Work(only remove project A from project B's BuildPath and add A's jar file).
Exception which throw is:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/tools/ant/util/FileUtils
at org.apache.jasper.JspC.resolveFile(JspC.java:1602)
at org.apache.jasper.JspC.setOutputDir(JspC.java:863)
at org.apache.jasper.JspC.setArgs(JspC.java:276)
at org.apache.jasper.JspC.main(JspC.java:241)
at jspCompiler.pars(jspCompiler.java:65)
at jspCompiler.main(jspCompiler.java:100)
at maintst.main(maintst.java:11)
Caused by: java.lang.ClassNotFoundException: org.apache.tools.ant.util.FileUtils
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)
... 7 more
Where is problem?
EDIT
Maintst.java
import jspParser.jspCompiler;
public class maintst {
public static void main(String[] args) throws Exception {
jspCompiler cmp=new jspCompiler();
cmp.main(new String[]{"F:\\arshad droos\\Thesis\\secureBranch\\personalblog\\build","F:\\arshad droos\\Thesis\\secureBranch"});
}
}
Browsing the code of Apache Tomcat i could not find the find org.apache.tools.ant.util.FileUtils. Since running from eclipse works, this probably means is that Project A has some jar in the build path which has this file. Find it, put in in the build path of Project B and things should work.
Eclipse has a fatjar named plugin. After installing this jar to eclipse plugins(Copy past jar file to eclipse's plugin directory) In Export menu select jar fat. In this way size of exported jar is big But Standalone.
Thank you.
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)
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...)