Intellij's artifacts and log4j - how to run

2.2k views Asked by At

Hi, i use Intellij since 5 days, and it looks pretty good. So i created a new java project and use log4j2, easy business, everything works fine. Then i created an artifact for easy deployment. The problem, i'am not able to run / execute the artifact JAR file! I get the error, taht the log4j.xml is missing:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/logging/log4j/LogManager
        at iBox.IBox.<init>(IBox.java:64)
        at iboxapp.Start.main(Start.java:28)
Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.LogManager
        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)

Log4j.xml is part of the artifact (JAR file). Where is the correct location for the xml file?! I don't use Maven, Intellij 2017.2

Any ideas?!

Thx

2

There are 2 answers

0
Sascha On

The problem was a mix of old and new log4j references. After cleanup, everything works fine. THX

0
eekboom On

First of all you should use a proper build system instead of relying on the IDE. I recommend gradle if you start from scratch.

Now that this is said: The error does not complain about missing xml file, but about missing class file.

How did you create the artifact? For testing I just

  • created a Java project in IDEA
  • added library dependencies to log4j-api and log4j-core, version 2.8.2
  • added a main class that just logs a single line using log4j
  • added an log4j2.xml to the root of the "src" folder
  • created an artifact:
    • Project Settings -> Artifacts -> "+" -> Jar -> from modules with dependencies
    • selected the main class
    • and kept the "JAR files from libraries" -> extract to the target JAR" selected
  • build the artifact: Build -> Build Artifacts
  • executed the artifact from the out/artifacts/test-artifact folder using "java -jar test-artifact"

Works without problems.