当我尝试使用 Maven 定义的依赖项运行应用程序时,出现 ClassNotFoundException
和 NoClassDefFoundError
异常。
我使用以下声明将相关 jar 的 Maven 依赖项添加到我的 pom.xml
文件中:
<dependency>
<groupId>spy</groupId>
<artifactId>spymemcached</artifactId>
<version>2.8.4</version>
<scope>provided</scope>
</dependency>
这会将相关的 JAR 文件添加到 Eclipse 中的 Maven 依赖项文件夹中。我可以访问代码中的类,但是一旦运行应用程序,我就会遇到上述异常。
该 jar 在我的 Java 构建路径中的 Maven 依赖项下引用:
我的本地 Maven 存储库已添加到我的类路径中:
当我尝试运行该应用程序时,出现以下两个异常:
java.lang.NoClassDefFoundError: Lnet/spy/memcached/MemcachedClient;
java.lang.ClassNotFoundException: net.spy.memcached.MemcachedClient
有人能指出我正确的方向吗?
最佳答案
将provided
更改为compile
已提供
This is much like compile, but indicates you expect the JDK or a container to provide the dependency at runtime. For example, when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.
关于具有 Maven 依赖的 Java ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42132517/