【发布时间】:2020-07-01 09:53:18
【问题描述】:
我试图使用来自org.apache.commons.codec 的Base32。下载包后,我按照 this guide(part 5 of 5: Adding External JARs) 将 JARS 添加到 Eclipse 中的项目构建路径中。我输入了我的代码,IDE 识别包中的类没有问题:
import java.security.SecureRandom;
import org.apache.commons.codec.binary.*;
public class Authenticator {
public static String generateSecretKey() {
SecureRandom generator = new SecureRandom();
byte[] bytes = new byte[20];
generator.nextBytes(bytes);
Base32 base32 = new Base32();
return base32.encodeToString(bytes);
}
public static void main(String[] args) {
generateSecretKey();
}
}
当我尝试运行我的代码时,我收到了这个错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/codec/binary/Base32
at Authenticator.generateSecretKey(Authenticator.java:11)
at Authenticator.main(Authenticator.java:17)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.codec.binary.Base32
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)
... 2 more
我尝试在the guide 中使用不同的方法来添加我的 JAR,但没有成功。我也尝试了this thread提出的解决方案,但仍然遇到同样的错误。
非常感谢任何帮助!
编辑: 如果有任何帮助,这是我的 .classpath 文件
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-14">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="/Library/commons-codec-1.14/commons-codec-1.14-javadoc.jar"/>
<classpathentry kind="lib" path="/Library/commons-codec-1.14/commons-codec-1.14-sources.jar"/>
<classpathentry kind="lib" path="/Library/commons-codec-1.14/commons-codec-1.14-test-sources.jar"/>
<classpathentry kind="lib" path="/Library/commons-codec-1.14/commons-codec-1.14-tests.jar"/>
<classpathentry kind="lib" path="/Library/commons-codec-1.14/commons-codec-1.14.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
【问题讨论】:
-
您有
module-info.java文件吗?如果是这样,请尝试删除它。 -
我的项目目录中没有“module-info.java”文件
-
你加了哪些罐子?
-
@nitind 我编辑我的帖子以包含 .classpath 文件。我添加了 5 个列出的 .jars
-
您可以安全地远程测试测试、源代码和 javadoc jar。它们不包含运行时代码。