Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ClassNotFound Exception while using Log4j in maven

 
Anudeep Duvvuri
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18020
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you included log4j as a dependency in your pom.xml?
 
Anudeep Duvvuri
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya have included the dependency in pom.xml
 
Anudeep Duvvuri
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya have included the dependency in pom.xml
 
Aniruddh Joshi
Ranch Hand
Posts: 275
Eclipse IDE jQuery Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please post the code in your pom.xml ?
 
Jaikiran Pai
Marshal
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Peter Johnson
author
Bartender
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic