wood burning stoves 2.0*
The moose likes Ant, Maven and Other Build Tools and the fly likes ClassNotFound Exception while using Log4j in maven Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "ClassNotFound Exception while using Log4j in maven" Watch "ClassNotFound Exception while using Log4j in maven" New topic
Author

ClassNotFound Exception while using Log4j in maven

Anudeep Duvvuri
Greenhorn

Joined: May 18, 2012
Posts: 29
HI i am new to maven as well as log4j . I have implemented loggers in my maven application. have set my classpath as well as environmental variables. while running my program using $java -cp target/school-0.0.1-SNAPSHOT.jar nbos.school.App
The following exception is arising

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at nbos.school.App.<clinit>(App.java:9)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger

while i am executing with the following command its working fine

$ mvn exec:java -Dexec.mainClass=nbos.school.App


here is my source code



any one please help me out.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15952
    
  19

Have you included log4j as a dependency in your pom.xml?


Customer surveys are for companies who didn't pay proper attention to begin with.
Anudeep Duvvuri
Greenhorn

Joined: May 18, 2012
Posts: 29
ya have included the dependency in pom.xml
Anudeep Duvvuri
Greenhorn

Joined: May 18, 2012
Posts: 29
ya have included the dependency in pom.xml
Aniruddh Joshi
Ranch Hand

Joined: Jul 29, 2008
Posts: 275

Could you please post the code in your pom.xml ?

Anrd
"One of the best things you could do is to simplify a larger application into a smaller one by reducing its process and complexity - Fowler"
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9930
    
159

Anudeep Duvvuri wrote: while running my program using $java -cp target/school-0.0.1-SNAPSHOT.jar nbos.school.App
The following exception is arising

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
at nbos.school.App.<clinit>(App.java:9)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger



The pom.xml won't come into picture if you aren't using Maven command to run that class. Why are you trying to run it outside of the mvn command, when like you say:

while i am executing with the following command its working fine

$ mvn exec:java -Dexec.mainClass=nbos.school.App


If you still want to run it outside of the Maven environment using the java command then your -cp should point to all the library jars that are required by that class at runtime.

[My Blog] [JavaRanch Journal]
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5812
    
    7

Where is the log4j.xml file located on your system? If it's only in the local Maven repository, then you could use something like this (assuming you are running on Linux and are using the default local repository location, and guessing as to which version of log4j you included):

java -cp target/school-0.0.1-SNAPSHOT.jar:~/.m2/repository/log4j/log4j/1.2.16/log4j-1.2.16.jar nbos.school.App


JBoss In Action
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

When you use

mvn exec:java

the dependencies for that project will be added to the classpath.

However, using

java -cp XXX

XXX will define the classpath for execution.

 
 
subject: ClassNotFound Exception while using Log4j in maven
 
Similar Threads
logging lifecyle events of servlet
Any body use Log4J with Tomcat?
sevlet error : Error allocating a servlet instance
Setting up JMSAppender with Websphere 5.0
Eclipse, jars, and noClassDefFoundError