当我尝试使用 maven 定义的依赖项运行我的应用程序时,我收到 ClassNotFoundException
和 NoClassDefFoundError
异常。
我将我的 Maven 依赖项添加到我的 pom.xml
文件中,声明如下:
<dependency>
<groupId>spy</groupId>
<artifactId>spymemcached</artifactId>
<version>2.8.4</version>
<scope>provided</scope>
</dependency>
这会将相关的 JAR 文件添加到我在 Eclipse 中的 Maven Dependencies 文件夹中。我可以在代码中访问这些类,但是一旦我运行应用程序就会得到上述异常。
在我的 Java 构建路径中引用了 Maven 依赖项下的 jar:
我的本地 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/12811392/