具有maven依赖性的Java ClassNotFoundException [英] Java ClassNotFoundException with maven dependency
问题描述
当我尝试使用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.
在Maven依赖项下的Java构建路径中引用了jar:
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?
推荐答案
将提供
更改为编译
已提供
这与编译很相似,但表示您希望JDK或容器在运行时提供依赖关系。例如,在为Java Enterprise Edition构建Web应用程序时,您可以将Servlet API和相关Java EE API的依赖性设置为提供的范围,因为Web容器提供了这些类。此范围仅在编译和测试类路径中可用,并且不可传递。
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!