Java Library Commons Lang3 ‘ClassNotFoundException’ error

huangapple go评论75阅读模式
英文:

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();

    }
}

在尝试运行此代码时,我收到以下错误:

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

我有两个文件,位于同一个包中。我只显示了这一个文件,因为我不认为另一个文件是问题所在。
我按照一个关于如何使用Java库的教程进行操作:下载、导入、构建路径等等。

问题在于,导入似乎是正确的,但实际使用该库是问题所在。

我对Java非常陌生,所以如果这是一个非常简单的错误,请原谅。

提前感谢您的任何响应/反馈。

英文:
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.

答案1

得分: 2

你正在使用的引用库(apache common lang3),以及任何其他库,都会以三种不同的方式使用。

  1. 首先,在开发过程中,您需要这个库,以便在您从库中调用类、对象和方法时,您的集成开发环境(IDE)可以验证您的代码。
  2. 在编译过程中,您需要这个库,这样Java编译器可以引用正确的路径,并在可能的情况下优化您的代码。
  3. 在运行时,当Java虚拟机运行您的程序时,您需要这个库,以便它可以找到您从库中使用的内容。

前两者通常被视为一种,因为两者通常都被认为是“编译时”,尽管严格来说,只有第二种实际上是编译时。这意味着您需要为IDE(适用于第1和第2点)和程序(第3点)准备好库。您的异常是由于在运行时,类加载器未找到您的库。类加载器是JVM加载程序所使用的类的方式。如果JVM找不到一个类,它无法继续执行您正在运行的线程,而您可能只在运行一个线程(主线程)。

因此,您的程序中断并停止运行。请查看您使用的关于如何正确导入库或将库导出到与您导出的JAR文件相邻的“lib”文件夹的教程。

编辑:在使用最新版本的Eclipse,并将项目导出为可运行的JAR文件时,会询问您想要如何处理库:

(图片已省略)

如果您在导出对话框的此子部分中看不到这个选项,那么您可能在做一些错误的操作(可能是您没有导出为可运行的JAR文件)。

英文:

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:

Java Library Commons Lang3 ‘ClassNotFoundException’ error

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

huangapple
  • 本文由 发表于 2020年5月29日 17:51:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/62083179.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定