• Post Reply Bookmark Topic Watch Topic
  • New Topic

ClassNotFound Exception while using Log4j in maven  RSS feed

 
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
Bartender
Posts: 18662
71
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
Sheriff
Posts: 10447
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: 5856
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
  • Likes 1
  • 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.

 
ramesh merugu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:Have you included log4j as a dependency in your pom.xml?


I have included dependency like this but same error, please help me
<dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>${log4j2.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-1.2-api</artifactId>
        <version>${log4j2.version}</version>
    </dependency>
 
Stephan van Hulst
Saloon Keeper
Posts: 7722
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can't help you if you don't tell us how you run your program and what error it is showing.
 
ramesh merugu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:We can't help you if you don't tell us how you run your program and what error it is showing.


Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
        at com.middleoffice.webservices.DriverScript.<clinit>(DriverScript.java:
47)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
        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
this error is using command java -jar inside-target-jar.jar UAT
I have included above dependencies aswell but I am not able to find the solution
 
ramesh merugu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ramesh merugu wrote:
Stephan van Hulst wrote:We can't help you if you don't tell us how you run your program and what error it is showing.


Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
        at com.middleoffice.webservices.DriverScript.<clinit>(DriverScript.java:
47)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
        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
this error is using command java -jar inside-target-jar.jar UAT
I have included above dependencies aswell but I am not able to find the solution.I am using maven pom.xml.Commads are mvn clean install.
 
ramesh merugu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ramesh merugu wrote:
ramesh merugu wrote:
Stephan van Hulst wrote:We can't help you if you don't tell us how you run your program and what error it is showing.


Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
        at com.middleoffice.webservices.DriverScript.<clinit>(DriverScript.java:
47)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
        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
this error is using command java -jar inside-target-jar.jar UAT
I have included above dependencies aswell but I am not able to find the solution.I am using maven pom.xml.Commads are mvn clean install.
 
ramesh merugu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:We can't help you if you don't tell us how you run your program and what error it is showing.


Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Logger
        at com.middleoffice.webservices.DriverScript.<clinit>(DriverScript.java:
47)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Logger
        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
this error is using command java -jar inside-target-jar.jar UAT
I have included above dependencies aswell but I am not able to find the solution.I am using maven pom.xml.Commads are mvn clean install.
 
Stephan van Hulst
Saloon Keeper
Posts: 7722
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at Jaikiran Pai's answer from five years ago.
 
Dana Ucaed
Ranch Hand
Posts: 353
6
Netbeans IDE Oracle Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is defined  {log4j2.version}?
 
ramesh merugu
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dana Ucaed wrote:Where is defined  {log4j2.version}?

My jars in lib folder.
log4j.jar
log4j-core-2.9.0.jar
log4j-1.2-api-2.3.jar

I have added dependencies like this
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
<scope>system</scope>
<systemPath>${jar_path}log4j.jar</systemPath>
   </dependency>
<dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>${log4j2.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-1.2-api</artifactId>
        <version>${log4j2.version}</version>
    </dependency>

I have ececuted below command aswell mvn exec:java -Dexec.mainClass=com.middleoffice.webservices.DriverScript
added this plugin for this..
<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.1</version>
  <executions><execution>
    <goals><goal>java</goal></goals>
  </execution></executions>
  <configuration>
    <mainClass>com.middleoffice.webservices.DriverScript</mainClass>
  </configuration>
</plugin>
mojo.JPG
[Thumbnail for mojo.JPG]
before adding the exec pugin this error occured
mojo2.JPG
[Thumbnail for mojo2.JPG]
after adding the exec plugin
 
Stephan van Hulst
Saloon Keeper
Posts: 7722
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you read the error message and did you do what it suggested?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!