0

I'm running some very old legacy code, which has worked in the past. This time, it's giving me the following error

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/LogManager

this error appears when I try to run a class from a .jar

  • 2
    might be that the jar containing that class is not on the path – Stultuske May 11 '23 at 13:12
  • how could I add it to the path? – Victor Miranda May 11 '23 at 13:16
  • It is a log4j class. Share the version expected and what build system does it uses to build, for example ant or maven. – aled May 11 '23 at 13:27
  • You've got two (actually three but let's forget the third for the moment) options: 1. Don't use `-jar` and specify the whole classpath, including the log4j jar(s) or 2. Make a 'fat jar' with log4j in it – g00se May 11 '23 at 13:34
  • Verify the Log4j configuration file named log4j.properties or log4j.xml is present in your project's classpath and also is being loaded by application. – Soheil Babadi May 11 '23 at 13:40
  • *Verify the..* @SoheilBabadi That won't help. Those configuration files are used *by* `LogManager` which is precisely the class that can't be found – g00se May 11 '23 at 13:54
  • @VictorMiranda - *is* this actually a Maven project? – g00se May 11 '23 at 15:21

1 Answers1

1

org.apache.log4j.LogManager is a class from log4j library.

Please check whether you have this log4j.jar in your classpath (reference pom.xml/build.gradle) and if it's missing add it to dependencies, e.g.

<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>

P.S. If the dependency is already there and you run the application as a separate jar file you might need to make the dependecy available at runtime with e.g. shading. See answer here log4j2 java.lang.NoClassDefFoundError: org/apache/logging/log4j/LogManager

P.P.S. The idea is to include missing dependency into classpath of your application.

Sergey Tsypanov
  • 3,265
  • 3
  • 8
  • 34
  • *this error appears when I try to run a class from a .jar* That's a *runtime* error and is therefore nothing to do with Maven (which hasn't even been *mentioned*) dependencies. It's being run "from a .jar" – g00se May 11 '23 at 14:06
  • @g00se yeah, I've updated my answer. Probably, the jar should include all the required dependencies either as shaded classes or as included jars. – Sergey Tsypanov May 11 '23 at 14:08
  • I would also update your answer on the Maven front too. Unless you exhort the OP to *use* Maven it's likely not relevant as it's "some very old legacy code". Maven isn't that old – g00se May 11 '23 at 14:11
  • This dependency won't help solve the error, because you're providing a Log4j 2 dependency, while the class in question is from log4j 1.x, which uses a different package namespace. – Mark Rotteveel May 11 '23 at 14:31
  • @MarkRotteveel it's log4j 1.x as you mentioned. – Sergey Tsypanov May 11 '23 at 14:32
  • @g00se well, it might be Ant as well. My idea is about to include the dependency into classpath somehow – Sergey Tsypanov May 11 '23 at 14:33
  • 1
    Then your answer should be updated to refer to `log4j:log4j:1.2.17`. – Mark Rotteveel May 11 '23 at 14:35
  • 1
    You only changed the version, but not the groupId and artifactId, which are different for version 1.x compared to 2.x. – Mark Rotteveel May 11 '23 at 14:40