问题描述
当我尝试使用 Maven 定义的依赖项运行我的应用程序时,我收到 ClassNotFoundException
和 NoClassDefFoundError
异常.
I am getting ClassNotFoundException
and NoClassDefFoundError
exceptions when I attempt to run my application using a maven defined dependency.
我使用以下声明将有关 jar 的 maven 依赖项添加到我的 pom.xml
文件中:
I added my maven dependency for the jar in question to my pom.xml
file with the following declaration:
<dependency>
<groupId>spy</groupId>
<artifactId>spymemcached</artifactId>
<version>2.8.4</version>
<scope>provided</scope>
</dependency>
这将相关的 JAR 文件添加到 Eclipse 中的 Maven Dependencies 文件夹中.我可以在代码中访问这些类,但是一旦我运行应用程序,我就会得到提到的异常.
This added the relevant JAR file to my Maven Dependencies folder in Eclipse. I can access the classes in code but I get the mentioned exceptions once I run the application.
jar 在我的 Java 构建路径中的 Maven 依赖项下引用:
The jar is referenced in my Java build path under Maven dependencies:
我的本地 Maven 存储库已添加到我的类路径中:
My local maven repository is added to my classpath:
当我尝试运行应用程序时,出现以下两个异常:
When I attempt to run the application, I get the following two exceptions:
java.lang.NoClassDefFoundError: Lnet/spy/memcached/MemcachedClient;
java.lang.ClassNotFoundException: net.spy.memcached.MemcachedClient
谁能指出我正确的方向?
Can anyone point me in the right direction?
推荐答案
将provided
改为compile
提供
这很像编译,但表明您希望 JDK 或容器在运行时提供依赖项.例如,在为 Java Enterprise Edition 构建 Web 应用程序时,您可以将 Servlet API 和相关 Java EE API 的依赖项设置为提供的范围,因为 Web 容器提供这些类.此范围仅在编译和测试类路径上可用,不可传递.
这篇关于具有 maven 依赖性的 Java ClassNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!