1

I am trying to get log4j2 working in Netbeans. No matter what I do, I get the following errors

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/logging/log4j/LogManager
at java.lang.ClassLoader.defineClass1(Native Method)

...

Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.LogManager
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)14
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)

I am following the instructions here Setting up log4j2 and trying to run the same demo program.

I have tried to find the solution and found this posting. Similar Problem here

This post Class not found is an exact duplicate of my problem, but no effective answer was posted.

I have added log4j-core-2.8.2.jar and log4j-1.2-api-2.8.2.jar to the project Libraries. These two files are installed in a sub-folder of the main src folder so they are on the class path.

Netbeans is installed on a new Ubuntu build. The frustrating thing is that I was able to get this running on a Windows machine.

I tried creating a fresh new project with only this logdemo class. I added the log4j2 jar files to the Library. I also added the command -Dlog4j.configurationFile=/home/test/config/log4j2.xml to the Run> VM Options box.

I still get exactly the same error message.

I checked my installation.

dazz@mimir:~$ java -version

java version "1.8.0_144" Java(TM) SE Runtime Environment (build 1.8.0_144-b01) Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

I found this post on setting Java Environment in Ubuntu. I would have thought that using apt-get would have setup the environment, but I may be wrong.

I checked /etc/environment . No mention of Java there. No java path exported in .bash.rc either. No java in /etc/profile.

There seems to be a number of places where the path can be set. I found this post Setting PATH variables in Ubuntu. It is modern and helpful. I also found this post Setting Java path variables in Ubuntu

I added the following to my home profile ~/.profile

JAVA_HOME=/usr/lib/jvm/default-java
PATH=$PATH:$HOME/bin:$JAVA_HOME/bin
export JAVA_HOME
export PATH

Note the use of default-java. This is a symbolic link to the JDK. I hope that if/when I upgrade to the next version, apt-get will change the link.

After a reboot of Ubuntu to see if this fixed the problem, no change.
Any suggestions to find the cause of the problem would be welcome.

dazz
  • 119
  • 2
  • 14

1 Answers1

2

I installed Netbeans on my Windows 10 PC and still had the very same symptoms so I figured the problem must be with the jar files.

I found the solution. I was using log4j-core-2.8.2.jar and log4j-1.2-api-2.8.2.jar instead of log4j-api-2.3.jar and log4j-core-2.3.jar.

For whatever reason the 2.3 version works and the 2.8.2 version doesn't. When I changed the jar files the program compiled and ran under both Win 10 and Ubuntu without error.

I am very annoyed about this. I am learning Java and I like the language but I have spent far more time troubleshooting issues than writing code. The demo programm compiled OK and only threw up errors when run. The experienced programmers that make Stackoverflow such a great resource didn't spot the problem, and why should they. The cryptic error messages gave no hint to the root cause of the problem. It shouldn't be this difficult. It is very frustrating.

dazz
  • 119
  • 2
  • 14