GS2VT
GS2VT

Reputation: 39

Java Library Commons Lang3 'ClassNotFoundException' error

import java.util.Arrays;
import org.apache.commons.lang3.ArrayUtils;

public class MonsterGame {

    public static void main(String[] args)
    {

        Monster.buildBattleBoard();

        char[][] tempBattleBoard = new char[10][10];

        // ObjectName[] ArrayName = new ObjectName[4];

        Monster[] Monsters = new Monster[4];

        // Monster(int health, int attack, int movement, String name)

        Monsters[0] = new Monster(1000, 20, 1, "Frank");
        Monsters[1] = new Monster(500, 40, 2, "Drac");
        Monsters[2] = new Monster(1000, 20, 1, "Paul");
        Monsters[3] = new Monster(1000, 20, 1, "George");

        Monster.redrawBoard();


    for (Monster m : Monsters) {
        if(m.getAlive()) {
            int arrayItemIndex = ArrayUtils.indexOf(Monsters, m);
            m.moveMonster(Monsters, arrayItemIndex);
        }
    }

    Monster.redrawBoard();


}
}

When trying to run this code, I get this error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/ArrayUtils
    at MonsterGame.main(MonsterGame.java:55)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.ArrayUtils
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:602)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    ... 1 more

I have two files, in the same package. I've only shown this one because I do not believe the other file is the problem. I followed a tutorial on how to use java libraries: download, import, build path etc.

The problem here is, the import seems to be fine but actually using the library is the problem.

I'm very new to Java so sorry if this is a very simple error to fix.

Thank you for any response/feedback in advance.

Upvotes: 2

Views: 2753

Answers (1)

TreffnonX
TreffnonX

Reputation: 2930

The referenced library you are using (apache common lang3) and any other library for that matter is used in three different ways.

  1. First, you need the library during development, so your IDE can validate your code, when you call classes, objects and methods from the library.
  2. During compilation you need the library, so the java compiler can reference the right paths, and optimize your code, where possible.
  3. You need the library during runtime, when your program is run by the Java Virtual Machine, so it can find whatever you used from the library.

The first 2 are usually seen as one, because both is usually considered 'compile time', though strictly speaking only the second one actually is. This means that you need to have the library in place for the IDE (for points 1 and 2) and for the program (point 3). Your exception is thrown, because during runtime, your library is not found by the ClassLoader. The ClassLoader is the way the JVM loads classes for the programs it uses. If the JVM does not find a class, it cannot continue to execute the Thread you are running, and you are probably only running one Thread (a main thread).

Therefore your program breaks, and stops running. Please either recheck the tutorial you are using on how to correctly import libraries or export the library to the lib folder next to the jar you are exporting.

Edit: When using an up to date version of eclipse, and exporting a project as runnable jar, you are asked what way you want to handle libraries:

enter image description here

If you do not see this subsection of the export dialog, you are doing something wrong (probably you are not exporting as runnable jar).

Upvotes: 2

Related Questions